COSC 3351 Software Design. Design Patterns Structural Patterns (I)

Similar documents
ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Design Patterns IV Structural Design Patterns, 1

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Object-Oriented Oriented Programming

Object-Oriented Oriented Programming Adapter Pattern. CSIE Department, NTUT Woei-Kae Chen

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

Adapter & Facade. CS356 Object-Oriented Design and Programming November 19, 2014

Object-Oriented Oriented Programming Adapter Pattern

SDC Design patterns GoF

CISC 322 Software Architecture

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Adapter Pattern Structural

Design Patterns in C++

Design Pattern: Composite

Design Patterns. GoF design patterns catalog

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Review of last class. 1.Class inheritance versus interface inheritance 2.Inheritance versus composition. Dr. Xiaolin Hu

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Component ConcreateComponent Decorator ConcreateDecoratorA ConcreteDecoratorB

Topics in Object-Oriented Design Patterns

Tecniche di Progettazione: Design Patterns

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

2.1 Design Patterns and Architecture (continued)

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

The Composite Design Pattern

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

2.1 Design Patterns and Architecture (continued)

UNIT I Introduction to Design Patterns

Information systems modelling UML and service description languages

Design Patterns Reid Holmes

Software Engineering

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns! Acknowledgements!

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

A Reconnaissance on Design Patterns

Design Patterns. An introduction

Foundations of Software Engineering Structural Design Patterns

Design Patterns. Decorator Pattern. Ekim 2017

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

Design Patterns V Structural Design Patterns, 2

Design Patterns. Definition of a Design Pattern

COURSE 4 DESIGN PATTERNS

CS 2340 Objects and Design

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Design for change. You should avoid

UNIT I Introduction to Design Patterns

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Software Design COSC 4353/6353 D R. R A J S I N G H

Design Patterns in Python (Part 2)

INSTITUTE OF AERONAUTICAL ENGINEERING

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Reuse at Design Level: Design Patterns

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Composite Pattern. IV.4 Structural Pattern

Introduction to Software Engineering: Object Design I Reuse & Patterns

Using Design Patterns in Java Application Development

CSE870: Advanced Software Engineering (Cheng) 1

Design Patterns: Structural and Behavioural

Introduction and History

Lecture 20: Design Patterns II

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design patterns. OOD Lecture 6

Laboratorio di Sistemi Software Design Patterns 2

Creational Design Patterns

Lecture 13: Design Patterns

Design Patterns Revisited

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Design Patterns Reid Holmes

C++ for System Developers with Design Pattern

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Software Engineering I (02161)

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

DESIGN PATTERNS Course 4

Design patterns. Jef De Smedt Beta VZW

SOFTWARE PATTERNS. Joseph Bonello

Design patterns generic models

An Introduction to Patterns

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Behavioral patterns. Command Iterator Observer Strategy Template method

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

COSC 3351 Software Design. Design Patterns Structural Patterns (II) Edgar Gabriel. Spring Decorator

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Transcription:

COSC 3351 Software Design Design Patterns Structural Patterns (I) Spring 2008 Purpose Creational Structural Behavioral Scope Class Factory Method Adaptor(class) Interpreter Template Method Object Abstract Factory Adaptor(object) Chain of Responsibility Builder Bridge Command Prototype Composite Iterator Singleton Decorator Mediator Façade Flyweight Proxy Observer State Strategy Visitor Memento 1

Structural Patterns Deal with how classes and objects are composed to form larger structures Structural class patterns use inheritance to compose interfaces or implementations Structural object patterns describe ways how to compose objects realizing new functionality; mainly use composition Adapter Intent: convert the interface of a class into another interface a client expects. Adapter lets classes work together that couldn t work else because of incompatible interfaces Also known as Wrapper Applicability: Use the adapter pattern when You want to use an existing class and its interface does not match the one you need You want to create a reusable class that cooperates with unrelated or unforeseen classes 2

Structure of class adapter using multiple inheritance Client Target + Request() Adaptee + SpecificRequest() Adapter + Request() Adapter:: Request() { SpecificRequest(); Structure of object adapter using object inheritance Client Target + Request() Adaptee + SpecificRequest() Adapter + Request() Adapter:: Request() { adaptee->specificrequest(); 3

Participants Target: defines the domain specific interface that Clients use Client: collaborates with objects conforming to the Target interface Adaptee: defines an existing interface that needs adapting Adapter: adapts the interface of Adaptee to the Target interface Consequences Class adapter: Adapts Adaptee to a concret Adaptee class-> class adapter won t work when we want to adapt a class and all its subclasses Object adapter: Works with many adaptees Adaptee and all its subclasses Overriding Adaptee behavior requires subclassing adaptee and make Adapter refer to subclass 4

Two way adapters Adapters are not necessarily transparent to all clients, since the adapted object no longer confirms to adapted interface Two-way adapters make sure, that both Adaptee and Client can modify/manipulate the object Example Consider a drawing editor with support for lines, polygons, text etc. Interface for graphical objects are provided by an abstract Class called Shape Editor defines subclasses for each object, e.g. PolygonShape, LineShape etc. Subclass for text very complex -> would like to use an existing toolkit (TextView) Interfaces do not match 5

Class Adapter class Shape { public: Shape(); virtual void BoundingBox (Point& bottemleft, Point& topright) const; virtual Manipulator* CreateManipulator() const; class TextView { public: TextView(); void GetOrigin (Coord& x, Coord& y) const; void GetExtent (Coord& width, Coord& height) const; virtual bool IsEmpty(); class TextShape: public Shape, private TextView { public: TextShape(); virtual void BoundingBox(Point& bottemleft, Point& topright) const; virtual bool IsEmpty(); virtual Manipulator* CreateManipulator() const; Class Adapter void TextShape:: BoundingBox (Point& bottemleft, Point& topright) const { Coord bottom, left, width, height; GetOrigin(bottom, left); GetExtent(width, height); bottomleft = Point(bottom, left); topright = Point(bottom+height, left+width); bool TextShaoe:: IsEmpty() const { return TextView::IsEmpty(); // This function is not supported by TextView, so we have to // implement it ourselves Manipulator* TextShape:: CreateManipulator () const { return new TextManipulator(this); 6

Object Adapter class TextShape: public Shape { public: TextShape(TextView*); virtual void BoundingBox(Point& bottemleft, Point& topright) const; virtual bool IsEmpty(); virtual Manipulator* CreateManipulator() const; private: TextView* _text; TextShape:: TextShape (TextView* t) { _text = t; void TextShape:: BoundingBox (Point& bottemleft, Point& topright) const { Coord bottom, left, width, height; _text->getorigin(bottom, left); _text->getextent(width, height); bottomleft = Point(bottom, left); topright = Point(bottom+height, left+width); Bridge Intent: Decouple an abstraction from its implementation so that the two can vary independently Applicability: use the bridge pattern when You want to avoid permanent binding between abstraction and its implementation Both abstraction and implementation should be extensible by subclassing Changes in the implementation of an abstraction should have no impact on the clients 7

Structure Client Abstraction + Operation() Implementor + OperationImp() Refined Abstraction + Operation() ConcreteImplementor A + OperationImp() ConcreteImplementor B + OperationImp() imp->operationimp(); Participants Abstraction: defines the abstraction interface; maintains a reference to an object of type Implementor RefinedAbstraction: extends the interface defined by Abstraction Implementor: defines the interface for implementation classes. Please note, that this interface does not have to correspond exactly to the Abstraction s interface ConcreteImplementator: implements the Implementor interface 8

Example: portable Window abstraction Bridge Window WindowImp + DrawText() + DrawRect() + DevDrawText() + DevDrawLine() Icon Window Transient Window XWindowsImp PMWindowsImp + DrawBorder() + DrawCloseBox() + DevDrawText() + DevDrawLine() + DevDrawText() + DevDrawLine() imp->devdrawline(); imp->devdrawline(); imp->devdrawline(); imp->devdrawline(); Difference between Bridge and Adapter The Adapter pattern Is geared toward making unrelated classes work together It is usually applied to systems after they are designed The Bridge pattern Is used upfront in design to let abstractions and implementations vary independently 9

Composite Intent: Compose objects into tree structures to represent partwhole relationship The Composite patterns lets clients treat individual objects and composites uniformly Applicability: use the composite pattern when You want to represent part-whole hierarchies of objects You want clients to be able to ignore the difference between composition of objects and individual objects Structure Client Component + Operation() + AddComponent() + RemoveComponent() + GetChild(n) Leaf Composite + Operation() + Operation() + AddComponent() + RemoveComponent() + GetChild(n) 10

Participants Component: Declares the interface for objects in the composition Implements default behavior for the interface common to all classes Declares an interface for accessing and managing its child components (optional) defines an interface for accessing a component s parent in the recursive structure Leaf: defines behavior of primitive objects in the composition Composite: defines behavior for components having children Client: manipulates objects in the composition through the Component interface Implementation issues Explicit parent references Required if you might move up in the hierarchy, not down Simplifies deleting a component Declaring child operations Declared in the component interface: requires meaningful defaults for leafs Gives you transparency Costs you safety, since clients may try to do meaningless operations on leafs Declared in the composite interface: Gives you security Costs transparency 11

Implementation issues Should component interface contain the set of children? Putting the child pointer into the base class might incur a space penalty Child ordering If required, must be handled in the child access and management interface Caching: Can be introduced in the composite class to store information about the children, if you expect a large number of similar traversals Decorator Intent: attach additional responsibility to an object dynamically Applicability: use a Decorator To add responsibilities to individual objects dynamically and if you do not want to affect other objects For responsibilities that can be withdrawn When extensions by subclassing is impractical 12

Structure Component + Operation() ConcreteComponent Decorator + Operation() + Operation() Component->Operation() Concrete Decorator A + addedstate + Operation() Concrete Decorator B + Operation() + addedbehavior Participants Component: defines the interface for objects that can have responsibilities attached to them dynamically ConcreteComponent: defines an object to which additional responsibilities can be attached Decorator: maintains a reference to a Component object and defines interface that conforms to Component s interface ConcreteDecorator: adds responsibilities to a component 13

Example: using decorators in a graphics library class VisualComponent{ public: VisualComponent(); virtual void Draw(); virtual void Resize(); class Decorator : public VisualComponent { public: Decorator(VisualComponent*); virtual void Draw(); virtual void Resize(); private: VisualComponent* _component; void Decorator:: Draw () { _component->draw(); void Decorator:: Resize () { _component->resize(); Example: using decorators in a graphics library class class BorderDecorator:: public Decorator { public: BorderDecorator(VisualComponent*, int width); virtual void Draw(); private: void DrawBorder(int); int _width; void BorderDecorator:: Draw () { Decorator::Draw() DrawBorder(_width); Please note: from an identity point of view, a decorated component is not identical to the component itself You can not rely on object identity when using decorator 14