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

Similar documents
Design Patterns IV Structural Design Patterns, 1

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

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

Adapter Pattern Structural

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

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

Design Pattern: Composite

Tecniche di Progettazione: Design Patterns

Object-Oriented Oriented Programming Adapter Pattern

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

CISC 322 Software Architecture

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

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

Design Patterns in C++

Design Patterns V Structural Design Patterns, 2

Design Patterns. GoF design patterns catalog

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

CS 2340 Objects and Design

Object-Oriented Oriented Programming

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

The Composite Pattern

Topics in Object-Oriented Design Patterns

SDC Design patterns GoF

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

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

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

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

Design Patterns Reid Holmes

Foundations of Software Engineering Structural Design Patterns

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

Design Patterns in Python (Part 2)

Design Patterns Reid Holmes

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

Composite Pattern. IV.4 Structural Pattern

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

Design Patterns. Decorator Pattern. Ekim 2017

SOFTWARE PATTERNS. Joseph Bonello

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

The Composite Design Pattern

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Lecture 20: Design Patterns II

Adapter pattern. Acknowledgement: Freeman & Freeman

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

Design Patterns. Definition of a Design Pattern

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

Design for change. You should avoid

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

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

Software Quality Management

Design Patterns Revisited

Design Patterns. Decorator. Oliver Haase

Using Design Patterns in Java Application Development

Software Engineering

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Reuse at Design Level: Design Patterns

The Decorator Pattern. Design Patterns In Java Bob Tarr

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

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

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

Component ConcreateComponent Decorator ConcreateDecoratorA ConcreteDecoratorB

A Reconnaissance on Design Patterns

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

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

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

Design Patterns! Acknowledgements!

Object-Oriented Concepts and Design Principles

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

INTERNAL ASSESSMENT TEST III Answer Schema

COURSE 4 DESIGN PATTERNS

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Introduction and History

Tecniche di Progettazione: Design Patterns

Information systems modelling UML and service description languages

Organization of User Interface Software

EINDHOVEN UNIVERSITY OF TECHNOLOGY

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

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

Lecture 13: Design Patterns

Considerations. New components can easily be added to a design.

Design Patterns. An introduction

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

Second Midterm Review

The GoF Design Patterns Reference

Creational Design Patterns

Virtual Frameworks for Source Migration

Introduction to Software Engineering: Object Design I Reuse & Patterns

Decorator Pattern. CS356 Object-Oriented Design and Programming November 7, 2014

Design Patterns B. Reid Holmes. Material and some slide content from: - Head First Design Patterns Book - GoF Design Patterns Book

Software Engineering I (02161)

Tecniche di Progettazione: Design Patterns

DESIGN PATTERN - INTERVIEW QUESTIONS

Tuesday, October 4. Announcements

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Review Software Engineering October, 7, Adrian Iftene

Laboratorio di Sistemi Software Design Patterns 2

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

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

administrivia today UML start design patterns Tuesday, September 28, 2010

Design Patterns Design patterns advantages:

Transcription:

Structural Design Patterns, 1 1 COMP2110/2510 Software Design Software Design for SE September 17, 2008 2 3 Department of Computer Science The Australian National University 4 18.1 18.2 GoF Structural Design Patterns Structural Family of GoF Structural patterns are concerned with how classes and objects are composed to form larger structures. There are class structural pattersn and objects structural pattern. ADAPTER BRIDGE COMPOSITE DECORATOR FAÇADE FLYWEIGHT PROXY Structural class patterns use inheritance to compose interfaces or implementations (eg, multiple inheritance combining two or more classes into a single class with the properties of all its parents). Example: CLASS ADAPTER. Structural object patterns describe ways to compose objects to realize new functionality. The added flexibility of object composition comes from the ability to change the composition at run-time, which is impossible with static class composition. Example: COMPOSITE, OBJECT ADAPTER. 18.3 18.4

Intent: Convert the interface of a class into another interface clients expect. lets classes work together that couldn t otherwise because of incompatible interfaces. Also known as: WRAPPER Often an API or a toolkit class is not re-uasable simply because its interface doesn t match the specific interface an application requires. Use the pattern if: you want to use an existing class, and its interface does not match the one you need example Consider for example a drawing editor that lets users draw and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editor s key abstraction is the graphical object, which has an editable shape and can draw itself. The interface for graphical objects is defined by an abstract class called Shape. The editor defines a subclass of Shape for each kind of graphical object: a LineShape class for lines, a PolygonShape class for polygons... But a TextShape subclass that can display and edit text is considerably more difficult to implement, since even basic text editing involves complicated screen update and buffer management. you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don t necessarily have compatible interfaces An existing UI toolkit already provides a sophisticated TextView class for displaying and editing text. Ideally we would like to reuse TextView to implement TextShape, but the toolkit wasn t designed for Shape classes. So we can t use TextView and Shape objects together. Instead, we could define TextShape so that it adapts the TextView interface to Shape s. We can do this in one of two ways: (1) by inheriting Shape s interface and TextView s implementation or (2) by composing a TextView instance within a TextShape and implementing TextShape in terms of TextView s interface. These two approaches correspond to the class and object versions of the ADAPTER pattern. We call TextShape an adapter. 18.5 18.6 uses object composition: uses multiple inheritance to adapt one interface to another: Use the pattern if (in addition to the two other ADAPTER uses): you need to use several existing subclasses, but it s impractical or impossible (multiple inheritance is not allowed) to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class 18.7 18.8

Formal Participants Target defines the domain-specific interface that Client uses (Shape in the above example) Client collaborates with objects conforming to the Target interface (DrawingEditor) Adaptee defines an existing interface that needs adapting (TextView) adapts the interface of Adaptee to the Target interface (TextShape) Clients call operations on an instance. In turn, the adapter calls Adaptee operations that carry out the request. s Adapt a ArrayedStack and LinkedStack to Stack interface. An abstract class AStack has four methods: push() pop() top() isempty() and it has two subclasses ArrayedStack and LinkedStack. Only adapts a class and all its parents, not all its subclasses Lets override some of Adaptee s behavior Only one object, hence no additional pointer indirection is needed to get to the adaptee Lets a single work with many Adaptees Makes it harder to override Adaptee behavior as the may not know which Adaptee it is working with Harder to override Adaptee behavior (it will require subclassing Adaptee and making refer to the subclass rather than the Adaptee itself) Draw or sketch the solution and relate to the UML diagram of ADAPTER pattern for a class doscussion. A Queue is just like a stack, except that additions take place at the opposite end to removals. It s a first in first out (FIFO) structure where the stack is a last in first out (LIFO) structure. In fact what the boss wants is a new top class called Dispenser, that has Stack and Queue as subclasses. Draw or sketch the solution, relate to the UML diagram of ADAPTER pattern and BRIDGE pattern for class discussion separately. Pluggable (introduced in Smalltalk, seen in JUnit design) is an adapter which adapts its interface dynamically to one of the several classes. It can adapt to the classes s recognise (determined by the constructor, or setparametrs() methods; also via use of reflections). 18.9 18.10 and dynamics Intent: Attach additional responsibilities to an object dynamically. s provide a flexible alternative to subclassing for extending functionality. Also known as: WRAPPER (as ADAPTER don t confuse) This pattern describes how to add responsibilities to objects dynamically (inheritance would be static and often unwieldy solution). DECORATOR is a structural pattern that composes objects recursively to allow an open-ended number of additional responsibilities. For example, a object containing a user interface component can add a decoration like a border or shadow to the component, or it can add functionality like scrolling and zooming. We can add two decorations simply by nesting one object within another, and so on for additional decorations. To accomplish this, each object must conform to the interface of its component and must forward messages to it. The DECORATOR can do its job (such as drawing a border around the component) either before or after forwarding a message. Use the pattern: to add responsibilities to individual objects dynamically and transparently (ie, without assuming that the client knows the contarct of the class, or without affecting other objects) for responsibilities that can be withdrawn when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing The run-time view of the decorated object: Using DECORATOR is like changing the skin of an object, or donning another piece of cloth 18.11 18.12

Formal Participants Component defines the interface for objects that can have responsibilities added to them dynamically (should be kept lightweight to avoid burdening concrete decorator class with unnecessary stuff"; Component s operations must be forwarded) ConcreteComponent defines an object to which additional responsibilities can be attached maintains a reference to a Component object and defines an interface that conforms to Component s interface Concrete adds responsibilities to the component forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request (+) More flexibility than static inheritance (properties can be added and remove at run-time; properties can be added repeatedly (fat border) (+) Avoids feature-laden classes high up in the hierarchy ( lazy design ) ( ) A decorator and its component are not identical (cannot rely on object identity when you use decorators) ( ) Too many (potentially) little look-alike objects (which differ only in the way they are interconnected, not in their class or in the value of their variables; these systems maybe easy to customize by those who understand them, they can be hard to learn and debug) The DECORATOR is commonly used in basic system frameworks: GUI components (Java s Swing), streams (Java s IO), fonts etc Intent: compose objects into tree structures to represent part-whole hierarchies. lets clients treat individual objects and compositions of objects uniformly. This is known as recursive composition. Component declares the interface for objects in the composition implements default behavior for the interface common to all classes, as appropriate declares an interface for accessing and managing its child components (optional) defines an interface for accessing a component s parent in the recursive structure, and implements it if that s appropriate implements child-related operations in the Component interface Leaf represents leaf objects in the composition. A leaf has no children defines behavior for primitive objects in the composition defines behavior for components having children stores child components mplements child-related operations in the Component interface Client manipulates objects in the composition through the Component interface 18.13 18.14 UML Diagram Uses Use the pattern if: You want to represent part-whole hierarchies of objects You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in composite structure uniformly An example of a composite object and the book example: (+) It makes it easy to add a new kinds of components (+) It makes client simpler, since they do not have to know if they are dealing with a leaf or composite component ( ) It makes it harder to restrict the type of components of a composite (the design is overly general one cannot rely of the type system to enforce the inclusion constraints, and has to use run-time checks) Implementation issues A composite object knows its contained components (its children). Should components maintain a reference to their parent component? (depend on application, eg in CHAIN OF RESPONSIBILITY pattern) Transparency vs Safety where should the child management methods (add(), remove(), getchild()) be declared? In the component class (transparent, not safe, relies on run-time checks) or in composite class (gives compile-time safety, looses transparency leaf and composite have different interface). Should Component maintain the list of components that will be used by a composite object (should it be an instance variable of a component rather than composite?) Is child ordering important? (depends on application) How deleted components are dealt with (if no GC available) What is the best data structure to store components (depends on application) 18.15 18.16

Apply COMPOSITE PATTERN to work with arithmetic expressions built out of operators: + * / and constants. The operations that we are interested in are to evaluate arithmetic expressions, and print them, eg (1 + 2) * 4 Result: (1+2)*4=12 4 + ( 3 * 6 ) Result: 4+(3*6)=22 If successful, extend it to include another type of leaf variables. 18.17