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

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.
Inheritance Inheritance Reserved word protected Reserved word super
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
More about classes and objects Classes in Visual Basic.NET.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
The Bridge Pattern Guang Hu February Overview MotivationMotivation ParticipantsParticipants  Structure  Applicability  Benefits  Drawbacks 
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 22 Object-Oriented Design
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Design Patterns.
GRASP Principles. How to Design Objects The hard step: moving from analysis to design How to do it? –Design principles (Larman: “patterns”) – an attempt.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Case Studies on Design Patterns Design Refinements Examples.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
GoF Sections Design Problems and Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns V More Structural Patterns.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
The Bridge Pattern (Structural) “All problems in computer science can be solved by another level of indirection.” – Butler Lampson ©SoftMoore ConsultingSlide.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Chapter Ten The Bridge Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
By Robert Smith Feb 23,  The normal method of dealing with an abstraction having several implementations is through inheritance.  However, this.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Elaboration: Iteration 2. Elaboration: Iteration 2 Basics Iteration 1 ends with : All the software has been tested: The idea in the UP is to do early,
Design Patterns: MORE Examples
Factory Patterns 1.
Inheritance and Polymorphism
Behavioral Design Patterns
Software Design and Architecture
BRIDGE PATTERN.
CS 350 – Software Design Principles and Strategies – Chapter 14
Structural Patterns: Adapter and Bridge
Adapter Design Pattern
Decorator Pattern.
Presentation transcript:

The Bridge Pattern.

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

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.

Motivation

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

Structure

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

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

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

Example 1 This example will seem basic. However, it is an excellent example of following 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

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 I should 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.

Example1

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.

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.

Example 1 Java Code Fragments

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.

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)..

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

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

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?

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.

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

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.

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.

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

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

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.

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.

Example1

Added protected methods in shape that call methods in Drawing

.

Example1- Java code fragments

Java code for example1

Java code exmple1

Java Code for Example1

Java code for Example1 Java Code for Example1

Java code for Example1