Bridge Design Pattern

2,982 views
2,783 views

Published on

Bridge Design Pattern - Structural Pattern

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,982
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
99
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Bridge Design Pattern

  1. 1. 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
  2. 2. Motivation Write a program that will draw rectangles with either of two drawing programs:  drawing program 1 (DP1)  drawing program 2 (DP2)
  3. 3. 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.
  4. 4. Cont.. new requirement: asked to support a new shape - circle adding another level, called Shape, which derives the Rectangle and Circle class.
  5. 5. 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
  6. 6. 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.
  7. 7. Try another alternate hierarchy redundancy still there !
  8. 8. Step 1. Identify what is varying
  9. 9. Step 2. Represent the variations
  10. 10. Step3. Tie the class together
  11. 11. Step4. Expanding the design
  12. 12. Bridge – Structure
  13. 13. Bridge – Participants• Abstraction » Defines the abstractions interface » Maintains a reference to an object of type implementor• RefinedAbstraction » Extends the interface defined by Abstraction
  14. 14. 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
  15. 15. Demo Time!
  16. 16. 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.
  17. 17. 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.
  18. 18. Collaborations Abstraction forwards client requests to its Implementor object.
  19. 19. 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
  20. 20. Implementation Ditto from GoF Please!
  21. 21. 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.
  22. 22. Thank You

×