Bridge pattern


Published on

Bridge pattern

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Bridge pattern

  1. 1. Design PatternBridge PatternA Structural design pattern Copyright © Astha
  2. 2. The ProblemSuppose a task has been given of writing a program that will draw rectangleswith either of two drawing programs. It also have been told that wheninstantiate a rectangle, it will know whether it should use drawing program 1(DP1) or drawing program 2 (DP2).The rectangles are defined as two pairs of points, as represented in thefollowing figure. The differences between the drawing programs aresummarizedin Table below. X2, Y2 X1, Y1 Copyright © Astha
  3. 3. The Problem DP1 DP2 Used to draw a draw_a_line( x1, drawline( x1, x2, line y1, x2, y2) y1, y2) Used to draw a draw_a_circle( x, y, drawcircle( x, y, r) circle r)we don’t want the code that draws the rectangles to worry about whattype of drawing program it should use. It occurs to me that because therectangles are told what drawing program to use when instantiated, I canhave two different kinds of rectangle objects: one that uses DP1 and onethat uses DP2. Each would have a draw method but would implement itdifferently. Copyright © Astha
  4. 4. The Problem Copyright © Astha
  5. 5. The ProblemA straightforward approach: implementing two shapes and twodrawing programs. Copyright © Astha
  6. 6. The ProblemBridge in Action Copyright © Astha
  7. 7. IntentDecouple an abstraction from its implementation so that the two can varyindependently. Copyright © Astha
  8. 8. ApplicabilityUse the Bridge pattern when you want to avoid a permanent binding between an abstraction and itsimplementation. This might be thecase, for example, when the implementation must be selected or switched atrun-time. both the abstractions and their implementations should be extensible bysubclassing. In this case, the Bridgepattern lets you combine the different abstractions and implementations andextend them independently. changes in the implementation of an abstraction should have no impact onclients; that is, their code shouldnot have to be recompiled. Copyright © Astha
  9. 9. Structure Copyright © Astha
  10. 10. Participants• Abstraction (Window) o defines the abstractions interface. o maintains a reference to an object of type Implementor.• RefinedAbstraction (IconWindow) o Extends the interface defined by Abstraction.• Implementor (WindowImp) o defines the interface for implementation classes. This interface doesnt have to correspond exactly to Abstractions interface; in fact the two interfaces can be quite different. Typically the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.• ConcreteImplementor (XWindowImp, PMWindowImp) o implements the Implementor interface and defines its concrete implementation. Copyright © Astha
  11. 11. Collaborations• Abstraction forwards client requests to its Implementor object. Copyright © Astha
  12. 12. Consequences Decoupling interface and implementation. An implementation is not bound permanently to an interface. The implementation of an abstraction can be configured at run-time. Its even possible for an object to change its implementation at run-time. Improved extensibility. You can extend the Abstraction and Implementor hierarchies independently. Hiding implementation details from clients. You can shield clients from implementation details, like the sharing of Implementor objects and the accompanying reference count mechanism (if any). Copyright © Astha
  13. 13. Implementation1. Only one Implementor. In situations where theres only one implementation, creating an abstract Implementor class isnt necessary. This is a degenerate case of the Bridge pattern; theres a one-to-one relationship between Abstraction and Implementor. Nevertheless, this separation is still useful when a change in the implementation of a class must not affect its existing clients—that is, they shouldnt have to be recompiled, just relinked.2. Creating the right Implementor object. How, when, and where do you decide which Implementor class to instantiate when theres more than one? Copyright © Astha
  14. 14. AssignmentA document viewer has been made for an enterprise solution (desktopapplication), which supports viewing of some specific types of documents. Thesupporting types are HTML, Microsoft Word and Microsoft Excel.Underneath, the document viewer uses an abstract interface of the documentwhich contains methods to show the document in the viewer.Now the enterprise application is going to have a web solution where therewould be another document viewer (different in several aspects from theexisting one) with the same functionalities as it is now in the desktop version.Use the Bridge pattern to solve the problem in such a way that new documenttypes can be easily incorporated into the system and new type of documentviewer can also be incorporated. Copyright © Astha