17
BRIDGE PATTERN “ decoupling an abstraction from its implementation Jang, Taeksoon (NHN NEXT) Wednesday, March 26, 14

Bridge pattern for Dummies

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Bridge pattern for Dummies

BRIDGE PATTERN“ decoupling an abstraction from its implementation ”

Jang, Taeksoon (NHN NEXT)

Wednesday, March 26, 14

Page 2: Bridge pattern for Dummies

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

Page 3: Bridge pattern for Dummies

BRIDGE PATTERN DEFINED

- Separate abstraction and implementation

Class itself : AbstractionWhat class can do : Implementation

Wednesday, March 26, 14

Page 4: Bridge pattern for Dummies

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

Page 5: Bridge pattern for Dummies

EXAMPLE & CODE

[ Requirement 1 ]

“ I need a programthat can draw circles and rectangles. “

Wednesday, March 26, 14

Page 6: Bridge pattern for Dummies

EXAMPLE & CODE[ What You Do ]

Wednesday, March 26, 14

Page 7: Bridge pattern for Dummies

EXAMPLE & CODE

[ Requirement 2 ]

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

Wednesday, March 26, 14

Page 8: Bridge pattern for Dummies

EXAMPLE & CODE[ What You Do ]

in progress....

Wednesday, March 26, 14

Page 9: Bridge pattern for Dummies

HERE COMESBRIDGE PATTERN !!!

Wednesday, March 26, 14

Page 10: Bridge pattern for Dummies

EXAMPLE & CODE[ What You Do ]

Wednesday, March 26, 14

Page 11: Bridge pattern for Dummies

EXAMPLE & CODE

[ Requirement 3 ]

“ I honestly want Green color and Triangle. “(oh yes....)

Wednesday, March 26, 14

Page 12: Bridge pattern for Dummies

EXAMPLE & CODE[ What You Do ]

Wednesday, March 26, 14

Page 13: Bridge pattern for Dummies

EXAMPLE & CODEAbstraction

Implementation Client

Wednesday, March 26, 14

Page 14: Bridge pattern for Dummies

REAL WORLD EXAMPLE

- GUI FrameworkSeparate Window abstraction from

Window implementationfor Linux, Windows, or Mac OS.

Wednesday, March 26, 14

Page 15: Bridge pattern for Dummies

REAL WORLD EXAMPLEex) Java AWT (Abstract Window Toolkit)

Wednesday, March 26, 14

Page 16: Bridge pattern for Dummies

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

Page 17: Bridge pattern for Dummies

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