Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Plab – Tirgul 12 Design Patterns
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Prototype Pattern Intent:
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
Design Patterns and Graphical User Interfaces Horstmann ,
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Chapter 9: The Iterator Pattern
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
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.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
GoF: Document Editor Example Rebecca Miller-Webster.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
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.
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.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
S.Ducasse Stéphane Ducasse 1 Adapter.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Design Patterns: MORE Examples
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Introduction to Design Patterns
Iterator and Composite Design Patterns
More Design Patterns 1.
More Design Patterns 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Decorator Pattern Intent
Jim Fawcett CSE776 – Design Patterns Summer 2003
Decorator Pattern Richard Gesick.
Advanced Java Programming
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Structural Patterns: Adapter and Bridge
Decorator.
Decorator Pattern.
Software Design Lecture : 39.
Software Design Lecture 10.
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

Design Patterns

CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the components without revealing the structure of the aggregate –e.g. it would be nice to be able to write something like: for (Iterator i = s.iterator() i.hasNext();) { x=i.next();... –e.g. we may have a Set data structure and wish to examine the elements of the set without revealing whether they are stored as an array, linked list, etc. –Solution is to define an Iterator

CS351 - Software Engineering (AY2007)Slide 3 Iterator – object behavioral Synopsis: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation Context: useful for accessing the components of any data structure, e.g. set, list, tree Forces: Use the Iterator pattern: –to access an aggregate object’s contents without exposing its internal representation –to support multiple traversals of aggregate objects –to provide a uniform interface for traversing different aggregate structures (i.e. to support polymorphic iteration)

CS351 - Software Engineering (AY2007)Slide 4 Iterator – object behavioral Solution: –Iterator – defines an interface for accessing and traversing elements –ConcreteIterator (e.g. PTListIterator) – implements the Iterator interface; keeps track of the current position in the traversal of the aggregate –Aggregate – defines an interface for creating an Iterator object –ConcreteAggregate (e.g. PTList) – implements the Iterator creation interface to return an instance of the proper ConcreteIterator

CS351 - Software Engineering (AY2007)Slide 5 Example: a list iterator PTListIterator will need to have a reference to the list (as indicated by the arrow in the diagram) For scoping reasons (in Java), the PTListIterator will be declared as a public inner class of PTList—only then will the hidden components of PTList be accessible to the iterator, cf. friend in C++

CS351 - Software Engineering (AY2007)Slide 6 Iterator – object behavioral Consequences: There are 3 important consequences: –It supports variations in the traversal of an aggregate. Complex data structures may be traversed in many ways. Different iterators can be defined for different kinds of traversal –Iterators simplify the Aggregate interface. The Aggregate does not need to supply extra functions for iteration, since they are now available in a separate class. –More than one traversal can be pending on an aggregate. This is because each iterator keeps track of its own traversal state. The same effect would be more difficult if the aggregate stored the traversal state. You should query the robustness of the iterator – what happens if the aggregate is changed during traversal?

CS351 - Software Engineering (AY2007)Slide 7 Java code for an iterator Code added to the PTSet class import java.util.Iterator;// Java has the interface defined import java.util.Set; class PTSet implements Set {// class definition … public class ElementIterator implements Iterator { // use the Iterator interface int currelt = 0; public boolean hasNext() { // part of the Iterator interface return currelt < numElements(); } public Object next() { // part of the Iterator interface Object result = null; // result will require coercion if (hasNext()) { result = item(currelt); currelt = currelt+1; } return result; } public Iterator iterator() {// function to create an iterator return new ElementIterator(); } Generate iterator Use iterator Interface of Java

CS351 - Software Engineering (AY2007)Slide 8 Java code for an iterator Code added to the client class import java.util.Iterator; class Client { // class definition for some client … PTSet s = new PTSet(); // will process elements of set s … for (Iterator iter = s.makeElementIterator(); iter.hasNext(); ) { // generate the iterator Element e=(Element)iter.next(); …} … }

CS351 - Software Engineering (AY2007)Slide 9 Decorator Pattern Sometimes we want to add responsibilities to individual objects rather than every object of a specific class. For example, we may want to dynamically decorate a text window with scroll bars if the text exceeds the window size, but inheritance would provide scroll bars to every text window. We want the ability to dynamically add or remove a decoration. The decorator conforms to the interface of its component so that its presence is transparent to the user. The decorator forwards requests to the component and may perform additional actions before and/or after forwarding. Decorations can be nested recursively so that an unlimited number of responsibilities can be added (e.g., scroll bars, border and shadowing may all be separate decorations).

CS351 - Software Engineering (AY2007)Slide 10 Decorator Pattern Decorations can appear anywhere the base component can. In this way clients can not tell the difference between a decorated and an undecorated component, and hence they don’t (can’t) depend on the decoration. Decorations are often used with graphical user interfaces, but they can also be used to decorate data structures such as graphs (to assist with graph traversal), etc.

CS351 - Software Engineering (AY2007)Slide 11 Decorator Pattern - applicability Use Decorator to add responsibilities to individual objects dynamically and transparently without affecting other objects. Use Decorator for responsibilities that can be removed/withdrawn. Use Decorator when extension through subclassing is impractical or undesirable.

CS351 - Software Engineering (AY2007)Slide 12 Decorator Pattern - structure Component Operation() ConcreteComponent Operation() Decoration Operation() ConcreteDecoratorB Operation() AddedBehavior() ConcreteDecoratorA AddedState Operation()

CS351 - Software Engineering (AY2007)Slide 13 Decorator Pattern - structure Component: defines the interface for objects that can have responsibilities added to them dynamically. ConcreteComponent: defines an object to which additional responsibilties can be attached. Decorator: maintains a reference to a component object and defines an interface that conforms to the Component interface. ConcreteDecorator: adds responsibilities to the component. Collorations –Decorator forwards requests onto the Component object. It may optionally perform additional operations before and after forwarding the request.

CS351 - Software Engineering (AY2007)Slide 14 Decorator Pattern - consequences At least 2 key benefits and 2 liabilities. –More flexibility than static inheritance. –Avoids feature laden classes high up in the hierarchy. –A document and its component are not identical. –Lots of little objects.

CS351 - Software Engineering (AY2007)Slide 15 Decorator Pattern - implementation Interface conformance Omitting the abstract Decorator class. There is no need to define an abstract Decorator class when all you want is to add a single responsibility. Keeping component classes lightweight. Changing the skin of an object versus changing its guts - think of the decorator pattern as a skin over an object that changes its behavior.

CS351 - Software Engineering (AY2007)Slide 16 Decorator Pattern - sample code Assume that there is a Component class called VisualComponent. There we could write the following C++ code. 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; }; Decorator decorates the VisualComponent referenced by _component which is initialized in the constructor.

CS351 - Software Engineering (AY2007)Slide 17 Decorator Pattern – sample code Decorator provides a default implementation that simply passes the request to _component. void Decorator::Draw () { _component->Draw(); } void Decorator::Resize () { _component->Resize(); }

CS351 - Software Engineering (AY2007)Slide 18 Decorator Pattern – sample code Subclasses of Decorator define the specific decorations. class BorderDecorator : public Decorator { public: BorderDecorator(VisualComponent*, int borderwidth); virtual void Draw(); private: void DrawBorder(int); private: int _width; } void BorderDecorator::Draw () { Decorator::Draw(); DrawBorder(_width); } Similar classes for ScrollDecorator and DropShadowDecorator. These can be composed to combine effects.

CS351 - Software Engineering (AY2007)Slide 19 Decorator Pattern – sample code First we need a way to put a visual component into a window object. Let’s assume our Window class has a SetContents operation for this purpose. void Window::SetContents (VisualComponent* content){ // … } Now we can create a text view and put a window in it. Window* window = new Window; TextView* textView = new TextView; TextView is a VisualComponent, which lets us put it in the window. Window->SetContents(textView);

CS351 - Software Engineering (AY2007)Slide 20 Decorator Pattern – sample code But we want a bordered and scrollable TextView. So we need to decorate it accordingly before putting it into the window. Window->SetContents( new BorderDecorator( new ScrollDecorator(textView), 1 ) ); Because Window accesses its content through the VisualComponent interface, it is unaware of the decorators presence. Related patterns: Adapter, Composite, Strategy.