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.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

4. Object-Oriented Programming Procedural programming Structs and objects Object-oriented programming Concepts and terminology Related keywords.
JDBC Session 4 Tonight: Design Patterns 1.Introduction To Design Patterns 2.The Factory Pattern 3.The Facade Pattern Thursday & Next Tuesday: Data Access.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
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,
PATTERNS -STRUCTURAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Design Patterns. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
Chapter 22 Object-Oriented Design
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
Design Patterns Ric Holt & Sarah Nadi U Waterloo, March 2010.
Client/Server Software Architectures Yonglei Tao.
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.
Design Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design Adapter Pattern Façade pattern.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.
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.
Design Patterns: Structural Design Patterns
Computer Science Design Patterns Spring TR 9:30-10:45 AM - EB 0011 Instructor: Bill White Engineering Building 3041 (618)
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Computing IV Introduction to Design Pattern Xinwen Fu.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Computing IV Singleton Pattern Xinwen Fu.
Structural Design Patterns
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Creational Patterns
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.
Design Patterns Structural Patterns. Adapter Convert the interface of a class into another interface clients expect Adapter lets classes work together.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Adaptor Bridge Composite UNIT-IV1 Repeated key points for Structural Patterns (Intent, Motivation, Also Known As…) Code Examples Reference
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
CS 350 – Software Design The Adapter Pattern – Chapter 7 Gang of Four Definition: Convert the interface of a class into another interface that the client.
S.Ducasse Stéphane Ducasse 1 Adapter.
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,
Examples (D. Schmidt et al)
Object-Orientated Analysis, Design and Programming
Software Architecture and Quality BY
Describe ways to assemble objects to implement a new functionality
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Design Patterns C++ Java C#.
Introduction to Design Patterns
Behavioral Design Patterns
Design Patterns C++ Java C#.
Design Patterns with C# (and Food!)
object oriented Principles of software design
Adapter Pattern 1.
Design Patterns.
Structural Pattern part-I introduction
Structural Patterns: Adapter and Bridge
Adapter Design Pattern
Adapter Pattern Jim Fawcett
Adapter Pattern Jim Fawcett
Presentation transcript:

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 structures Adapter Intent: Convert the programming interface of one class into that of another so the two classes can work together in one program Bridge Intent: Separate the interface of a class from its implementation so that either can be varied independently Composite Intent: Develop a system in which a component may be an individual object or a collection of objects Decorator Intent: Modify the behavior of individual objects without creating a new derived class Façade Intent: Reduce the complexity of elaborate subsystems by providing a simplified interface to them Flyweight Intent: Reduce the number of similar classes by sharing their commonality and passing their differences as parameters Proxy Intent: Represent a complex object with a simpler one, thereby postponing the instantiation of the complex object until it is truly needed

Structural Pattern: Adapter When the client expects to interact with a class in a particular manner, it is sometimes necessary to “adapt” the interface of the class to accommodate those expectations. C h a p t e r 4 – P a g e 56 The client will call the adapter’s interface which, in turn, translates those calls into calls to the original interface of the class being adapted. This permits classes to work together in spite of incompatible interfaces.

The Adapter Pattern The Client has been set up to collaborate with objects that conform to the domain-specific Target interface. C h a p t e r 4 – P a g e 57 The existing Adaptee interface is adapted to the Target interface by means of the Adapter. This solution is frequently needed when an old software component offers useful functionality, but its interface is incompatible with the architecture of a new system that is being developed.

Non-Software Example The RoundHole client has been set up to check whether a RoundPeg target fits by checking its radius. C h a p t e r 4 – P a g e 58 The SquarePeg adaptee has a radius, not a width, but can be adapted to determine its “radius” by dividing its width by the square root of two. Note that the adaptee isn’t necessarily converted into an object of the target class, but it is adapted so the client’s needed functionality is available.

Primitive Software Example The client uses the (x,y) position of a Rectangle’s upper left corner and the Rectangle’s width and height to draw the Rectangle. C h a p t e r 4 – P a g e 59 The old LegacyRectangle uses the (x,y) positions of its upper left and lower right corners to draw it. The RectangleAdapter adapts the old-style drawing to the new style by converting the client’s information about the one corner, the width, and the height into the LegacyRectangle’s information about the two corners.

C++ Code for Rectangle Adapter C h a p t e r 4 – P a g e 60 #include using namespace std; typedef int Coordinate; typedef int Dimension; // Target Interface: Abstract Superclass class Rectangle { public: virtual void draw() = 0; }; // Adaptee: Old-style rectangle class LegacyRectangle { public: LegacyRectangle(Coordinate x1, Coordinate y1, Coordinate x2, Coordinate y2) { lowX = x1; lowY = y1; highX = x2; highY = y2; cout << "LegacyRectangle: create. (" (" << highX << "," << highY << ")" << endl; } void oldDraw() { cout << "LegacyRectangle: oldDraw. (" (" << highX << "," << highY << ")" << endl; }

C h a p t e r 4 – P a g e 61 private: Coordinate lowX; Coordinate lowY; Coordinate highX; Coordinate highY; }; // Adapter: Rectangle is the abstract class providing the interface, while // LegacyRectangle is the concrete class providing the implementation. class RectangleAdapter: public Rectangle, private LegacyRectangle { public: RectangleAdapter(Coordinate x, Coordinate y, Dimension w, Dimension h): LegacyRectangle(x, y, x + w, y + h) { cout << "RectangleAdapter: create. (" << x << "," << y << "), width = " << w << ", height = " << h << endl; } virtual void draw() { cout << "RectangleAdapter: draw." << endl; oldDraw(); } }; void main() { Rectangle *r = new RectangleAdapter(120, 200, 60, 40); r->draw(); }

C h a p t e r 4 – P a g e 62 Adapter Example: External Polymorphism Polymorphism may be implemented even if the classes are not derived from the same base class. The ExecuteAdapter adapts the three old-style classes (Moe, Larry, and Curly) to the new target style (ExecuteInterface) by converting mapping each of their respective methods (doThis, doThat, and doTheOther) to the target’s execute method.

C h a p t e r 4 – P a g e 63 External Polymorphism C++ Code #include using namespace std; // Target: Specifies the new interface class ExecuteInterface { public: virtual ~ExecuteInterface() {} virtual void execute() = 0; }; // Adapter: "Maps" the new interface to the legacy implementation template class ExecuteAdapter: public ExecuteInterface { public: ExecuteAdapter(TYPE *o, void(TYPE:: *m)()) { object = o; method = m; } ~ExecuteAdapter() { delete object; } void execute() { (object->*method)(); } private: TYPE *object; // ptr-to-object attribute void(TYPE:: *method)(); // ptr-to-member function attribute };

C h a p t e r 4 – P a g e 64 // Adaptees: Three totally incompatible classes with // no common base class and no hope of polymorphism class Moe { public: ~Moe() { cout << "Moe::destructor" << endl; } void doThis() { cout << "Moe::doThis()" << endl; } }; class Larry { public: ~Larry() { cout << "Larry::destructor" << endl; } void doThat() { cout << "Larry::doThat()" << endl; } }; class Curly { public: ~Curly() { cout << "Curly::destructor" << endl; } void doTheOther() { cout << "Curly::doTheOther()" << endl; } };

C h a p t e r 4 – P a g e 65 // An array of new interfaces is returned, adapted from the old implementations ExecuteInterface** initialize() { ExecuteInterface** array = new ExecuteInterface*[3]; array[0] = new ExecuteAdapter (new Moe(), &Moe::doThis); array[1] = new ExecuteAdapter (new Larry(), &Larry::doThat); array[2] = new ExecuteAdapter (new Curly(), &Curly::doTheOther); return array; } // The client uses the new interface, effectively producing an "external" polymorphism void main() { int i; ExecuteInterface** objects = initialize(); for (i = 0; i < 3; i++) objects[i]->execute(); for (i = 0; i < 3; i++) delete objects[i]; delete objects; }

Adapter Pattern Advantages C h a p t e r 4 – P a g e 66 The Adapter Design tends to make things work after they’ve already been designed, compelling normally incompatible designs to collaborate successfully. By adapting new interfaces to old implementations, the old code becomes reusable. In addition, the client is freed from the burden of having to account for object differences, since it treats all objects the same way. In addition, new types of objects can be accommodated without making changes to the client code.