bridge design pattern
Post on 10-May-2015
1.882 Views
Preview:
DESCRIPTION
TRANSCRIPT
• I N T E N T
» D E C O U P L E A N A B S T R A C T I O N F R O M I T S I M P L E M E N TAT I O N S O T H AT T H E T W O C A N VA RY I N D E P E N D E N T LY
» S E PA R AT E T H E A B S T R A C T I O N A N D I T S I M P L E M E N TAT I O N A N D H AV E S E PA R AT E I N H E R I TA N C E S T R U C T U R E F O R B O T H .
Design Pattern – Bridge(Structural)
• Also known as» Handle / Body
by
Shahriar HyderOct 05, 2011
Motivation
Write a program that will draw rectangles with either of two drawing programs:
drawing program 1 (DP1)
drawing program 2 (DP2)
DP 1 DP 2
draw a line draw_a_line( x1, y1, x2, y2) drawline( x1, x2, y1, y2)
draw a circle draw_a_circle( x, y, r) drawcircle( x, y, r)
The collection (the client of rectangles) doesn’t want to worry about what type of drawing program it should use.
Example
Cont..
new requirement:
asked to support a new shape - circle
adding another level, called Shape, which derives the Rectangle and Circle class. ?
new problems arise …
if I get another drawing program – DP3• I will have 6 different kinds of Shapes (two Shape concepts
times three drawing programs).
if I get another type of Shape• I will have nine different types of Shapes (three Shape
concepts times three drawing programs).
class explosion!
DP:Drawing Program S:Shapes Need ≈DP*S new classes Want ≈DP+S new classes The new classes are hard wired for each type of Drawing Programs
Problem
“Hardening of the software arteries” has occurred by using sub-classing 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.
Try another alternate hierarchy
redundancy still there !
Step 1. Identify what is varying
Step 2. Represent the variations
Step3. Tie the class together
Step4. Expanding the design
Bridge – Structure
Bridge – Participants
• Abstraction» Defines the abstractions interface
» Maintains a reference to an object of type implementor
• RefinedAbstraction» Extends the interface defined by
Abstraction
Bridge – Participants – 2
• Implementer » Defines the interface for implementation classes
> Can be different from the Abstraction interface
– Implementer provides primitive operations – Abstraction provides higher-level operations
• ConcreteImplementer » Implements the Implementer interface » Defines its concrete implementation
Demo Time!
Bridge – Applicability
• The normal method of dealing with an abstraction having several implementations is through inheritance.
• Avoid permanent binding between an abstraction and its implementation » Especially if selection or switching of implementation is at run-time rather than design time.
• Both abstractions and implementations should be extensible by sub-classing
• Changes in implementation should have no impact on clients• Share an implementation among multi objects and this fact
should be hidden from the client• You have a proliferation of classes resulting from a coupled
interface and numerous implementations• You need to map orthogonal class hierarchies.
The Bridge pattern is useful when you have an abstraction that has different implementations. It allows the abstraction and the implementation to vary independently of each other.
• Find what varies and encapsulate it.
• Favor composition over inheritance.
Bridge – Applicability Summary
Collaborations
Abstraction forwards client requests to its Implementor object.
Bridge – Consequences
• Decouples interface and implementation» Can configure implementation to use at runtime» Encourages better structure through layering
> The client only has to know about Abstraction
and Implementer
• Improved extensibility» Extend in Abstraction and Implementerindependently
• Hide implementation details from clients
Implementation
Ditto from GoF Please!
Bridge – Related Patterns
• Abstract Factory can create and configure a particular Bridge
• Adapter is geared toward making unrelated classes work together» Usually applied after systems are designed» Bridge is used during design
• Structural difference: Bridge can abstract a complex entity from its implementation; Adapter only abstracts a single interface
• A bridge is by design. An adaptor is not. An adaptor is a patch. A bridge is put in place on purpose.
Thank You
top related