1 / 44

The Bridge Pattern

The Bridge Pattern. Intent. Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body. Motivation.

Download Presentation

The Bridge Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Bridge Pattern .

  2. Intent • Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body

  3. Motivation • Problem: For some classes, we want to adapt (reuse) either their abstractions, their implementations or both. How can we structure a class so that abstractions and/or implementations can easily be modified. • Inheritance binds an implementation to the abstraction permanently, which makes it difficult to modify, extend, and reuse abstraction and implementations independently.

  4. Motivation

  5. Applicability • Need to avoid a permanent binding between an abstraction and implementation. • When abstractions and implementations should be extensible through subclassing. • When implementation changes should not impact clients. • When the implementation should be completely hidden from the client. (C++) • When you have a proliferation of classes. • When, unknown to the client, implementations are shared among objects

  6. Structure

  7. Participants & Collaborations • Abstraction - defines the abstraction's interface - maintains a reference to the Implementor - forwards requests to the Implementor (collaboration) • RefinedAbstraction - extends abstraction interface • Implementor - defines interface for implementations • ConcreteImplementor - implements Implementor interface, ie defines an implementation

  8. Consequences • Decouples interface and implementation Decoupling Abstraction and Implementor also eliminates compile-time dependencies on implementation. Changing implementation class does not require recompile of abstraction classes. • Improves extensibility Both abstraction and implementations can be extended independently • Hides implementation details from clients • More of a design-time pattern

  9. Implementation Issues • How, where, and when to decide which implementer to instantiate? Depends: - if Abstraction knows about all concrete implementer, then it can instantiate in the constructor. - It can start with a default and change it later - Or it can delegate the decision to another object (to an abstract factory for example) • Implementors can be shared and usage tracked. (Handle / Body) • Can’t implement a true bridge using multiple inheritance A class can inherit publicly from an abstraction and privately from an implementation, but since it is static inheritance it bind an implementation permanently to its interface

  10. Example 1 This example will seem basic. However, it is an excellentexample offollowing two of the mandates of the design pattern community: “find what varies and encapsulate it” and “favor object composition over class inheritance” Look at the concepts discussed in this example and then try to think of situations that you have encountered that are similar, having • Variations in abstractions of a concept, and • Variations in how these concepts are implemented. Bottom line: During requirements definition, explore for variations early and often

  11. Example1 Suppose I have been given the task of writing a program that will draw rectangles with either of two drawing programs. I have been told that when I instantiate a rectangle, I will know whether Ishould use drawing program 1 (DP1) or drawing program 2 (DP2). The rectangles are defined as two pairs of points, as represented in the following figures and the differences between the drawing programs are summarized in the following table.

  12. Example1

  13. Example 1 My customer told me that the clients of the rectangles do not want to worry about what type of drawing program it should use. It occurs to me that since the rectangles are told what drawing program to use when instantiated, I can have two different kinds of rectangle objects: one that uses DP1 and one that uses DP2. Each would have a draw method but would implement it differently.

  14. Example1 By having an abstract class Rectangle, I take advantage of the fact that the only difference between the different types of Rectangle are how they implement the drawLine method. The V1Rectangle is implemented by having a reference to a DP1 object and using that object’s draw_a_line method. The V2Rectangle is implemented by having a reference to a DP2 object and using that object’s drawline method. However, by instantiating the right type of Rectangle, I no longer have to worry about this difference.

  15. Example 1 Java Code Fragments

  16. Example 1 Now, suppose that after completing this code, one of the inevitable requirements three (death, taxes, and changing requirements) comes my way. I am asked to support another kind of shape—this time, a circle. However, I am also given the mandate that the collection object does not want to know the difference between Rectangles and Circles. It occurs to me that I can simply extend the approach I’ve already started by adding another level to my class hierarchy. I only need to add a new class, called Shape, from which I will derive the Rectangle and Circle classes. This way, the Client object can just refer to Shape objects without worrying about what kind of Shape it has been given. As a beginning object-oriented analyst, it might seem natural to implement these requirements using only inheritance. For example, I could start out with something like the following figure and then, for each kind of Shape, implement the shape with each drawing program, deriving a version of DP1 and a version of DP2 for Rectangle and deriving a version of DP1 and a version of DP2 one for Circle.

  17. Example1

  18. Example1 • The combinatorial problem Look at the previous class diagram and pay attention to the third row of classes. Consider the following: - The classes in this row represent the four specific types of Shapes that I have.What happens if I get another drawing program, that is, another variation in implementation? I will have six different kinds of Shapes (two Shape concepts times three drawing programs). - Imagine what happens if I then get another type of Shape, another variation in concept. I will have nine different types of Shapes (three Shape concepts times three drawing programs). .

  19. Example1 The class explosion problem arises because in this solution, the coupling abstraction(the kinds of Shapes)and the implementation(the drawing programs) are tightly coupled. Each type of shape must know what type of drawing program it is using. I need a way to separate the variations in abstraction from the variations in implementation so that the number of classes only grows linearly

  20. Example1 This is exactly the intent of the Bridge pattern: [to] de-couple an abstraction from its implementation so that the two can vary independently.2

  21. Example 1 Question: What else is poor about this design? • Does there appear to be redundancy? • Would you say things have high cohesion or low cohesion? • Are things tightly or loosely coupled? • Would you want to have to maintain this code?

  22. Example1 The overuse of inheritance. As a beginning object-oriented analyst, I had a tendency to solve the kind of problem I have seen here by using special cases, taking advantage of inheritance. I loved the idea of inheritance because it seemed new and powerful. I used it whenever I could. This seems to be normal for many beginning analysts, but it is naive: given this new “hammer,” everything seems like a nail.

  23. Example1 • An alternative approach May be we used the wrong type of inheritance hierarchy? Is this one any better?

  24. Example1

  25. Example1

  26. Example1 The characteristics of the problem fit this nicely. I know that I ought to be using the Bridge pattern even though I do not know yet how to implement it.

  27. Example1 • You have to do two things first. 1- Commonality analysis 2-Variablity analysis The common concepts will be represented by abstract classes , variations will be implemented by concrete classes.

  28. Example1 In this case we different types of shapes and different type of drawing programs. So common concepts are shape and Drawing. So we encapsulate them

  29. The next step is to represent the specific variations that are present. For Shape, rectangles and circles. For drawing programs,DP1 and DP2 respectively.

  30. Example1 • Lets see if we can relate these classes by having one use the other. Two possibilities: either Shape uses the Drawing programs or the Drawing programs use Shape. If drawing programs could draw shapes directly, then they would have to know some things about shapes in general: what they are, what they look like. But this violates a fundamental principle of objects: an object should only be responsible for itself. • It also violates encapsulation. Drawing objects would have to know specific information about the Shapes (that is, the kind of Shape) in order to draw them.

  31. Example1 • Now, consider the first case. Shapes use Drawing objects to draw themselves. Shapes wouldn’t need to know what type of Drawing object it used since I could have Shapes refer to the Drawing class.

  32. Example1

  33. Example1 Added protected methods in shape that call methods in Drawing

  34. .

  35. Example1- Java code fragments

  36. Java code for example1

  37. Java code exmple1

  38. Java Code for Example1

  39. Java code for Example1 • Java Code for Example1

  40. Java code for Example1

More Related