Design Pattern – Bridge (Structural)• INTENT» DECOUPLE AN ABSTRACTION FROM ITSIMPLEMENTATION SO THAT THE TWO CAN VARYINDEPENDENTLY» SEPARATE THE ABSTRACTION AND ITSIMPLEMENTATION AND HAVE SEPARATEINHERITANCE STRUCTURE FOR BOTH. • Also known as » Handle / Body by Shahriar Hyder Oct 05, 2011
Motivation Write a program that will draw rectangles with either of two drawing programs: drawing program 1 (DP1) drawing program 2 (DP2)
Example DP 1 DP 2draw 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 worryabout what type of drawing program it should use.
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). classDP:Drawing Program S:ShapesNeed ≈DP*S new classes explosion!Want ≈DP+S new classesThe 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 !
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
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.
Bridge – Applicability SummaryThe Bridge pattern is useful when you have anabstraction that has different implementations. Itallows the abstraction and the implementation tovary independently of each other. • Find what varies and encapsulate it. • Favor composition over inheritance.
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 Implementer independently• Hide implementation details from clients
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.