Bridge Pattern Derek Weeks

1,275 views

Published on

Published in: Business, Technology
  • Be the first to comment

  • Be the first to like this

Bridge Pattern Derek Weeks

  1. 1. Bridge Pattern Derek Weeks Melbourne Patterns Group 26 April 2006
  2. 2. Intent and Structure <ul><li>Decouple an abstraction from its implementation so that the two can vary independently. </li></ul>
  3. 3. Motivation (1) <ul><li>Example from Design Patterns Explained (Alan Shalloway and James Trott). </li></ul>Drawing a rectangle with two drawing programs, DP1 and DP2, by creating two subclasses of Rectangle . The implementation lives in these subclasses and is hidden from the client, but…
  4. 4. Motivation (2) Not so easy to add more shapes! m × n classes… We have hard-coded the abstraction (shape hierarchy) to the implementation (how the drawing is done).
  5. 5. Motivation (3) <ul><li>Redesign with the Bridge Pattern: </li></ul>
  6. 6. Sample code (1)
  7. 7. Shape code (2)
  8. 8. Sample code (3)
  9. 9. Advantages <ul><li>We want to favour composition over inheritance to achieve loose coupling. </li></ul><ul><ul><li>The Bridge Pattern does this well. </li></ul></ul><ul><li>The ‘things that vary’ are encapsulated: </li></ul><ul><ul><li>The abstraction (new shapes added). </li></ul></ul><ul><ul><li>The implementation (new drawing programs). </li></ul></ul><ul><li>The choice of implementation can be made at runtime/no recompile of client. </li></ul>
  10. 10. Implementation considerations <ul><li>May be no need to subclass Implementor </li></ul><ul><ul><li>But still allows implementation to change later. </li></ul></ul><ul><li>Who decides which Implementor to instantiate? </li></ul><ul><ul><li>The Abstraction class? </li></ul></ul><ul><ul><li>Some factory class? </li></ul></ul>
  11. 11. References <ul><li>Gamma, Erich et al. Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley, 1995. </li></ul><ul><li>Shalloway, Alan and Trott, James. Design Patterns Explained: A New Perspective on Object-Oriented Design . Addison-Wesley, 2001. </li></ul>

×