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

Bridge Pattern

Decouples an abstraction from its implementation


so that the two can vary independently.*

*GoF
Bridge Pattern

Decouples an abstraction from its implementation


so that the two can vary independently.* **

*GoF

**wtf?
Bridge Pattern
Separate a varying entity from a varying behavior
(separate an "abstraction" from its
"implementation"), so that these issues can vary
independently.

Sometimes we say it this way: separate what


something is from what it does, where both of
these things vary for different reasons.*

* Design Patterns Explained: A New Perspective on Object-


Oriented Design by Alan Shalloway and James R. Trott
Client
UML!!!

Implementor
Abstraction
+OperationImp()
+Operation()

Concrete Implementor A Concrete Implementor B

+OperationImp() +OperationImp()

Refined Abstraction A Refined Abstraction B

+Operation() +Operation()
An Example
Circle
+draw()
An Example
Shape
+draw()

Circle Rectangle

+draw() +draw()
An Example
Shape
+draw()

Circle Rectangle

+draw() +draw()

Vector Rectangle Bitmap Rectangle


Vector Circle Bitmap Circle
+draw() +draw()
+draw() +draw()
An Example
Shape
+draw()

Double Circle Circle Rectangle

+draw() +draw() +draw()

Vector DoubleCircle

+draw()

Vector Rectangle Bitmap Rectangle


Bitmap Double Circle Vector Circle Bitmap Circle
+draw() +draw()
+draw() +draw() +draw()
Client
OMG! Bridge!

Drawing Implementation
Shape
+drawRect()
(protected ) myDrawImp +drawCircle()
+Draw()

Vector Draw Bitmap Draw


+drawRect() +drawRect()
+drawCircle() +drawCircle()

Rect Circle Double Circle


+Draw() +Draw() +Draw()
Similar Patterns
● What patterns is it similar to?
– Strategy – but it supports varying the abstraction as
well as the implementation (behavior)
– Template – the abstraction can be handled like a
template
– It's kind of a combination of Strategy and Template
● What patterns does it frequently use?
– Adapter – often times the supported
implementations will have different interfaces

You might also like