<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>
1
NTUT CSIE
Bridge Pattern
Bridge Pattern
<b>CSIE Department, NTUT</b>
<b>Woei-Kae Chen</b>
2
NTUT CSIE
Bridge pattern: Intent
Bridge pattern: Intent
z
Decouple
an
abstraction
from its
implementation
so that the two can
vary independently
</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>
3
NTUT CSIE
Bridge pattern: Motivation
Bridge pattern: Motivation
z
When an abstraction can have several
possible implementations, the usual way to
accommodate them is to use inheritance.
Bridge pattern: Motivation
Bridge pattern: Motivation
z
Two drawbacks
– It’s inconvenient to extend the Window abstractionto
cover different kinds of windows.
– It makes client code platform dependent(client
instantiates a concrete implementation)
<b>Window</b>
<b>XWindow</b> <b>PMWindow</b>
<b>Window</b>
<b>XWindow</b> <b>PMWindow</b> <b>IconWindow</b>
</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>
5
NTUT CSIE
Bridge pattern: Motivation
Bridge pattern: Motivation
z
Bridge pattern: put the Window abstraction and its
implementation into separate class hierarchies
+DrawText()
+DrawRect()
<b>Window</b>
+DrawBorder()
<b>IconWindow</b>
+DrawCloseBox()
<b>TransientWindow</b>
DrawRect()
DrawText() DrawRect()
imp->DevDrawLine()
imp->DevDrawLine()
imp->DevDrawLine()
imp->DevDrawLine()
+DevDrawText()
+DevDrawLine()
<b>WindowImp</b>
+DevDrawText()
+DevDrawLine()
<b>XWindowImp</b>
+DevDrawText()
+DevDrawLine()
<b>PMWindowImp</b>
XDrawLine() XDrawString()
imp <b>bridge</b>
6
NTUT CSIE
Bridge pattern: Applicability
Bridge pattern: Applicability
z
Use the bridge pattern when
– You want to avoid a permanent binding between an
abstraction and its implementation
– Both the abstractions and their implementations should
be extensible by subclassing
– Changes in the implementation of an abstraction should
have no impact on clients – their code should not have
to be recompiled
– (C++) you want to hide the implementation of an
abstraction completely from clients (in C++, the
representation of a class is visible in the class interface)
</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>
7
NTUT CSIE
Bridge pattern: Structure
Bridge pattern: Structure
+Operation()
<b>Abstraction</b>
<b>RefinedAbstraction</b>
imp->OperationImp()
+OperationImp()
<b>Implementor</b>
+OperationImp()
<b>ConcreteImplementorA</b>
imp
+OperationImp()
<b>ConcreteImplementorB</b>
<b>Client</b>
Bridge pattern: Participants
Bridge pattern: Participants
z
Abstraction (Window)
– Defines the abstraction’s interface
– Maintains a reference to an object of type Implementor
z
RefinedAbstraction (IconWindow)
– Extends the interface defined by Abstraction
z
Implementor (WindowImp)
– Defines the interface for implementation classes
(typically, the Implementor interface provides only
primitive operations, and Abstraction defines
higher-level operations)
z
ConcreteImplementor
</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>
9
NTUT CSIE
Bridge pattern: Collaborations
Bridge pattern: Collaborations
z
Abstraction forwards client requests to its
implementor object
10
NTUT CSIE
Bridge pattern: Consequences
Bridge pattern: Consequences
z
<i><b>Decoupling interface and implementation</b></i>
–
The implementation of an abstraction can be
configured at run-time.
–
Eliminates compile-time dependencies between
client and implementation
z
<i><b>Improved extensibility</b></i>
–
Abstraction and Implementor hierarchies can be
extended independently
</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>
11
NTUT CSIE
Bridge pattern: Implementation
Bridge pattern: Implementation
z
<i><b>Only one Implementor</b></i>
–
This is a degenerate case. There is a one-to-one
relationship between Abstraction and
Implementor –
still useful
, when a change in
the implementation of a class must not affect its
existing clients
z
<i><b>Creating the right implementor object</b></i>
–
If Abstraction knows about all
ConcreteImplementor classes, it can instantiate
one of them in its constructor
–
Or, we can introduce a factory object whose
duty is to encapsulate platform-specifics
Bridge pattern: Related Patterns
Bridge pattern: Related Patterns
z
An Abstract Factory can create and
configure a particular Bridge
z
The
Adapter
pattern is geared toward
making unrelated classes work together. It
is usually applied to systems
after
they’re
designed.
Bridge
, on the other hand, is used
</div>
<!--links-->