Bridge pattern for Dummies

Post on 26-Jun-2015

839 views 2 download

Tags:

description

You can grasp the idea of bridge pattern and see real life examples in code and class diagrams.

Transcript of Bridge pattern for Dummies

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 : AbstractionWhat class can do : Implementation

Wednesday, March 26, 14

PARTICIPANT

Abstraction : defines abstract interface and maintains the implementor referenceRefined Abstraction : extends the interface defined by abstractionImplementor : defines the interface for implementation classesConcrete Implementor : implements the Implementor interface

Wednesday, March 26, 14

EXAMPLE & CODE

[ Requirement 1 ]

“ I need a programthat 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 COMESBRIDGE 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 & CODEAbstraction

Implementation Client

Wednesday, March 26, 14

REAL WORLD EXAMPLE

- GUI FrameworkSeparate Window abstraction from

Window implementationfor Linux, Windows, or Mac OS.

Wednesday, March 26, 14

REAL WORLD EXAMPLEex) 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