Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University 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.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
William Shiver.  Pattern used to decouple an abstraction from its implementation so that the two can vary independently.
The Bridge Pattern Guang Hu February Overview MotivationMotivation ParticipantsParticipants  Structure  Applicability  Benefits  Drawbacks 
Bridge The decoupling of abstraction and implementation.
Chapter 8, Object Design Introduction to Design Patterns
Design Patterns. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
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.
Reuse Activities Selecting Design Patterns and Components
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Introduction to Object-oriented programming and software development Lecture 1.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
Case Studies on Design Patterns Design Refinements Examples.
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.
Mediator Pattern and Multiuser Protection Billy Bennett June 8 th, 2009.
Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Introduction to Design Patterns.
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.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
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.
CS 5150 Software Engineering Lecture 16 Program Design 3.
SOLID Design Principles
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these.
Structural Patterns C h a p t e r 4 – P a g e 55 StructuralPatterns Design patterns that describe how classes and objects can be combined to form larger.
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
Design Patterns: MORE Examples
Inheritance ITI1121 Nour El Kadri.
Behavioral Design Patterns
Creational Pattern: Prototype
Polymorphism.
Review: Two Programming Paradigms
Object Oriented Analysis and Design
State Design Pattern 1.
Generation Gap By Kurt Rehwinkel
BRIDGE PATTERN.
Design Patterns.
Structural Pattern part-I introduction
Structural Patterns: Adapter and Bridge
Adapter Design Pattern
Principles of Object-Oriented Design
Decorator Pattern.
Lecture 6: Polymorphism
Presentation transcript:

Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult to independently extend either the interface or the implementation. C h a p t e r 4 – P a g e 67 The Bridge Pattern addresses this problem by decoupling the two class hierarchies, making them orthogonal rather than parallel. In this way, the abstract interfaces can be platform- independent even when the concrete implementations are platform-dependent.

The Bridge Pattern C h a p t e r 4 – P a g e 68 The Abstraction defines the client’s interface, maintaining a reference to an Implementor object, while the Refined Abstraction extends the Abstraction interface. The Implementor defines the interface for the implementation, providing primitive operations (while the Abstraction provides the higher-level operations based on these primitives). The ConcreteImplementors implement the Implementor class, providing the platform-specific implementation details.

Example: Without Bridge Pattern Three types of shapes, with each type split into two platform- dependent subtypes. C h a p t e r 4 – P a g e 69 Any change to one shape type (e.g., Circle) could extend its effects to the two platform- dependent subtypes (e.g., PlatformACircle, PlatformBCircle). Any change to a platform (e.g., PlatformA) could extend its effects to all three platform-dependent shape subtypes (e.g., PlatformACircle, PlatformARectangle, PlatformATriangle).

Example: With Bridge Pattern By decoupling the Shape abstraction from the Drawing implementor, the explosion of platform- dependent shape subclasses is avoided and the amount of redundant code is minimized. C h a p t e r 4 – P a g e 70 With this model, individual shape alterations and additional extensions to the Shape class will only affect the abstraction portion of the model, while platform modifications will only affect the implementor portion of the model.

Bridge Example: Time Zones The interface base class (Time) has a pointer to the TimeImplementor base class, and each class in the interface hierarchy (i.e., the Time class and its subclasses) is responsible for populating the base class pointer with the correct concrete implementor class C h a p t e r 4 – P a g e 71 At that point, all requests from the client are simply delegated by the interface class to the encapsulated implementor class.

C++ Code for Time Zone Bridge C h a p t e r 4 – P a g e 72 #include using namespace std; class TimeImplementor { public: TimeImplementor(int hr, int min) { hour = hr; minute = min; } virtual void tell() { cout << "time is " << setw(2) << hour << minute << endl; } protected: int hour, minute; }; class CivilianTimeImplementor: public TimeImplementor { public: CivilianTimeImplementor(int hr, int min, int pm): TimeImplementor(hr, min) { if (pm) whichM = " PM"; else whichM = " AM"; } void tell() { cout << "time is " << hour << ":" << minute << whichM << endl; } protected: string whichM; };

C h a p t e r 4 – P a g e 73 class MilitaryTimeImplementor: public TimeImplementor { public: MilitaryTimeImplementor(int hr, int min, int zone): TimeImplementor(hr, min) { switch (zone) { case 5: { timezone = " Eastern Standard Time"; break; } case 6: { timezone = " Central Standard Time"; break; } case 7: { timezone = " Mountain Standard Time"; break; } case 8: { timezone = " Pacific Standard Time"; break; } } void tell() { cout << "time is " << setw(2) << hour << minute << timezone << endl; } protected: string timezone; }; class Time { public: Time(){} Time(int hr, int min) { implementor = new TimeImplementor(hr, min); } virtual void tell() { implementor->tell(); } protected: TimeImplementor *implementor; };

C h a p t e r 4 – P a g e 74 class CivilianTime: public Time { public: CivilianTime(int hr, int min, int pm) { implementor = new CivilianTimeImplementor(hr, min, pm); } }; class MilitaryTime: public Time { public: MilitaryTime(int hr, int min, int zone) { implementor = new MilitaryTimeImplementor(hr, min, zone); } }; void main() { Time* times[3]; times[0] = new Time(14, 30); times[1] = new CivilianTime(2, 30, 1); times[2] = new MilitaryTime(14, 30, 6); for (int i = 0; i < 3; i++) times[i]->tell(); }

Bridge Pattern Advantages C h a p t e r 4 – P a g e 75 The Bridge Pattern is particularly useful when the structure of a class (its implementor) and what the class actually does (its abstraction) tend to change frequently. The Bridge Pattern is most appropriate when the software model possesses two orthogonal dimensions (e.g., “what the client wants” vs. “what the platform provides”, “front-end” vs. “back-end”, “domain” vs. “infrastructure”). By decoupling the interface from the implementation, the Bridge Pattern facilitates the independent extensibility of each and has the desirable effect of hiding details from the client.