Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
The Bridge Pattern Guang Hu February Overview MotivationMotivation ParticipantsParticipants  Structure  Applicability  Benefits  Drawbacks 
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm.
Bridge The decoupling of abstraction and implementation.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Chapter 8, Object Design Introduction to Design Patterns
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
1 Structural Patterns  How classes and objects are composed to form larger structures  Structural class patterns: use inheritance to compose interfaces.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Abstract Factory Design Pattern making abstract things.
Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson
GoF Sections Design Problems and Design Patterns.
Facade Introduction. Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
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.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
The Bridge Pattern (Structural) “All problems in computer science can be solved by another level of indirection.” – Butler Lampson ©SoftMoore ConsultingSlide.
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.
Adaptor Bridge Composite UNIT-IV1 Repeated key points for Structural Patterns (Intent, Motivation, Also Known As…) Code Examples Reference
By Robert Smith Feb 23,  The normal method of dealing with an abstraction having several implementations is through inheritance.  However, this.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by FACADE PATTERN
Design Patterns: MORE Examples
Structural Patterns How classes and objects are composed to form larger structures Structural class patterns: use inheritance to compose interfaces or.
Abstract Factory Pattern
Factory Method Pattern
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
Software Design and Architecture
Factory Method Pattern
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
More Design Patterns 1.
Chapter 8, Object Design Introduction to Design Patterns
Design Patterns Satya Puvvada Satya Puvvada.
More Design Patterns 1.
Behavioral and Structural Patterns
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
BRIDGE PATTERN.
Structural Pattern part-I introduction
Structural Patterns: Adapter and Bridge
Adapter Design Pattern
Software Design Lecture : 28.
Presentation transcript:

Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern

Intent Decouple an abstraction from its implementation so that the two can vary independently. Also Know AS  Handle/Body

Motivation When an abstraction can have one of several possible implementations, the usual way to accommodate them is to use inheritance. Inheritance binds an implementation to the abstraction permanently, which makes it difficult to modify, extend and reuse abstractions and implementations independently.

Motivation(2) Consider the implementation of a portable Window abstraction in a user interface toolkit. Using inheritance has two drawbacks:  It ’ s inconvenient to extend the Window abstraction to cover different kinds of windows or new platforms.  It makes client code platform-dependent.

Motivation(3) The Bridge pattern addresses these problems by putting the Window abstraction and its implementation in separate class hierarchies.  There is one class hierarchy for Window interface.  and a separate hierarchy for platform-specific window implementations, with WindowImp as its root.

Motivation(4) All operations on Window subclasses are implemented in terms of abstract operations from the WindowImp interface. This decouples the window abstractions from the various platform-specific implementations. We refer to the relationship between Window and WindowImp as a Bridge.

Applicability Use the Bridge pattern when  you want to avoid a permanent binding between an abstraction and its implementation.  both the abstractions and their implementations should be extensible by subclassing.  changes in the implementation of an abstraction should have no impact on clients.  You want to hide the implementation of an abstraction completely from clients.

Applicability(2) Use the Bridge pattern when(2)  you have a proliferation of classes as shown earlier in the first Motivation diagram.  You want to share an implementation among multiple objects, and this fact should be hidden from the client.

Structure bridge structure diagram Abstraction Operation() RefinedAbstraction Implementor OperationImp() ConcreteImplementorA OperationImp() ConcreteImplementorB OperationImp()

Participants Abstraction (Window)  defines the abstraction ’ s interface.  maintains a reference to an object of type Implementor RefinedAbstraction  Extends the interface defined by Abstraction

Participants(2) Implementor (WindowImp)  defines the interface for implementation classes.  This interface doesn ’ t have to correspond exactly to Abstraction ’ s interface.  Typically the Implementor interface provides only primitive operations, and Abstraction defines higher- level operations based on these primitives. ConcreteImplementor  implements the Implementor interface and defines its concrete implementation.

Collaborations Abstraction forwards client requests to its Implementor object.

Consequences(1) Decoupling interface and implementation  An implementation is not bound permanently to an interface.  Decoupling Abstraction and Implementor also eliminates compile-time dependencies on the implementation.  This decoupling encourages layering that can lead to a better-structured system.

Consequences(2) Improved extensibility.  You can extend the Abstraction and Implementor hierarchies independently. Hiding implementation details from clients  You can shield clients from implementation details.

Implementation(1) Only one Implementor.  An abstract Implementor class isn ’ t necessary. (A degenerate case of the Bridge pattern).  The separation is still useful when a change in the implementation of a class must not affect its existing clients.  In C++, the class interface of the Implementor class can be defined in a private header file. This lets you hide an implementation of a class completely from its clients.

Implementation(2) Creating the right Implementor object  How, when, and where do you decide which Implementor class to instantiate when there ’ s more than one?  If Abstraction knows about all ConcreteImplementor classes, it can instantiate one of them in its constructor according to the parameter.  Another approach: choose a default and changes it later.  It also possible to delegate the decision to antoher object altogether.

Implementation(3) Sharing implementors  Using a reference count.  The sample code. Handles& Handle::operator= (const Handle& other) { other._body->Ref(); _body->Unref(); if(_body->RefCount() == 0) {delete _body;} _body = other._body; return *this; }

Implementation(4) Using multiple inheritance  You can use multiple inheritance in C++ to combine an interface with its implementation. A class can inherit publicly from Abstraction and privately from a ConcreteImplementor.  But it binds an implementation permanently to its interface.

Sample Code class Window { public window(View * contents); virtual void DrawContents(); virtual void open(); virtual void close(); virtual void Iconify(); virtual void DrawLine(); virtual void DrawRect(); … }

Sample Code class WindowImp{ public: virtual void ImpTop() = 0; virtual void ImpBottom() = 0; virtual void ImpSetExtent(const Point&) = 0; virtual void ImpSetOrigin(const Point&) = 0; … }

Sample Code Subclasss of Window define the different kinds of windows. class ApplicationWindow : public Window { public: virtual void DrawContents(); } void ApplicationWindow::DrawContents(){ GetView()->DrawOn(this); }

Sample Code class IconWindow : public Window { public: virtual void DrawContents(); private: const char * _bitmapName; } void IconWindow::DrawContents(){ WindowImp* imp = GetWindowImp(); if(imp != 0) imp->DeviceBitmap( … ); }

Sample Code Window operations are defined in terms of the WindowImp interface: void Window::DrawRect(const Point& p1, const Point& p2) { WindowImp *imp = GetWindowImp(); imp->DeviceRect(p1.X(), p1.Y(), p2.X(), p2.Y()); }

Sample Code class XWindowImp: public WindowImp{ public: XWindowImp(); virtual voidDeviceRect(coord, Coord, Coord, Coord); private: … }

Sample Code WindowImp * Window::GetWindowImp() { if(_imp == 0) { _imp = WindowSystemFactory::Instance()- >MakeWindowImp(); } return _imp; }

Related Patterns Abstract Factory can create and configure a particular Bridge. The Adapter(139) patterns is geared toward making unrelated classes work together.