Bridge Using Electrical
Bridge Using Electrical
Bridge
Object Diagram for
Bridge using Electrical
Switch Example
Intent
– Decouple an abstraction from its implementation
so that the two can vary independently
Also Known As
Handle/Body
Problem
"Hardening of the software arteries" has occurred by using
subclassing of an abstract base class to provide alternative
implementations. This locks in compile-time binding between
interface and implementation. The abstraction and
implementation cannot be independently extended or composed.
Structural Bridge (151)
Illustration: Problem
Consider the implementation of a portable Window
abstraction in a user interface toolkit. This abstraction
should enable us to write applications that work on both x
window system and IBM’s PM .
Illustration: Problem
Draw Backs:
1> It’s inconvenient to extend the Window
abstraction to cover different kinds of windows or
new platform ( ie. Every new platform a new
Window subclass for every Kind of Window is
necessary )
2> It makes client code platform-dependent.
Whenever a client creates a window, it instantiates a
concrete class that has specific implementation. This,
in turn, makes it harder to port the client code to
other platforms.
Structural Bridge (151)
Illustration:
Solution
The client interacts with the interface class, and it in turn "delegates" all
requests to the implementation class.
The interface object is the "handle" known and used by the client; while the
implementation object, or "body", is safely encapsulated to ensure that it
may continue to evolve, or be entirely replaced (or shared at run-time
Structural Bridge (151)
Applicability
• Use the Bridge pattern when:
you want run-time binding of the implementation and want to
avoid permanent binding between an abstract and its
implementation.
Both the abstraction and their implementations should be
extensible by subclassing.
Change in the implementation of an abstraction should have
no impact on client; ie their code should not be recomplied
you want to share an implementation among multiple objects,
you have a proliferation of classes resulting from a coupled
interface and numerous implementations
you need to map orthogonal class hierarchies.
Structural Bridge (151)
Structure
Collaborations
Abstraction forwards client request to its
implementers object.
Structural Bridge (151)
Consequences include:
decoupling the object's interface & implementation