Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

BRIDGE PATTERN

“ decoupling an abstraction from its implementation ”

Jang, Taeksoon
(NHN NEXT)

Wednesday, March 26, 14


MOTIVATION
- Suppose an abstraction should have different implementation

- Simple implementation would just extend the object itself

- Then, inheritance binds an implementation to the abstraction

- When a class changes often, you should change all classes

“ Bridge Pattern helps minimize changes in class ”

Wednesday, March 26, 14


BRIDGE PATTERN DEFINED

- Separate abstraction and implementation

Class itself : Abstraction


What class can do : Implementation

Wednesday, March 26, 14


PARTICIPANT

Abstraction : defines abstract interface and maintains the implementor


reference
Refined Abstraction : extends the interface defined by abstraction
Implementor : defines the interface for implementation classes
Concrete Implementor : implements the Implementor interface
Wednesday, March 26, 14
EXAMPLE & CODE

[ Requirement 1 ]

“ I need a program
that can draw circles and rectangles. “

Wednesday, March 26, 14


EXAMPLE & CODE
[ What You Do ]

Wednesday, March 26, 14


EXAMPLE & CODE

[ Requirement 2 ]

“ I want shapes to be colorful, blue and red! ”

Wednesday, March 26, 14


EXAMPLE & CODE
[ What You Do ]

in progress....

Wednesday, March 26, 14


HERE COMES
BRIDGE PATTERN !!!

Wednesday, March 26, 14


EXAMPLE & CODE
[ What You Do ]

Wednesday, March 26, 14


EXAMPLE & CODE

[ Requirement 3 ]

“ I honestly want Green color and Triangle. “


(oh yes....)

Wednesday, March 26, 14


EXAMPLE & CODE
[ What You Do ]

Wednesday, March 26, 14


EXAMPLE & CODE
Abstraction

Implementation Client

Wednesday, March 26, 14


REAL WORLD EXAMPLE

- GUI Framework
Separate Window abstraction from
Window implementation
for Linux, Windows, or Mac OS.

Wednesday, March 26, 14


REAL WORLD EXAMPLE
ex) Java AWT (Abstract Window Toolkit)

Wednesday, March 26, 14


BENEFIT OF BRIDGE PATTERN

- Avoid binding between abstraction and implementation


=> Able to select implementation at run time

- Reduction in the number of sub classes

- Abstraction and Implementation can be varied independently

- Cleaner code without ‘if ’ or ‘switch’ statement

Wednesday, March 26, 14


ADAPTER VS BRIDGE

Adapter
- meant to change the interface of an existing object
- intended to make unrelated classes work together
- My Code + Someone else’s Code

Bridge
- intended to decouple abstraction from its implementation
- Entirely My Code and My Structure

Wednesday, March 26, 14

You might also like