Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.

Slides:



Advertisements
Similar presentations
Creating Graph Cards Graphical representation of XA Data Jim Boyer
Advertisements

XP 1 Developing a Basic Web Site Tutorial 2: Web Site Structures & Links.
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Chapter 3: The Decorator Pattern
System Sequence Diagrams
Winter 2007ACS-3913 Ron McFadyen1 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners)
Microsoft Office 2003 Illustrated Brief Elements to a Document Adding Special.
Unified Modeling Language Philip Liew
CS 210 Introduction to Design Patterns September 12 th, 2006.
Jan 29, Ron McFadyen1 UML Class Diagram Examples Based on well-known patterns Exhibit ways of providing dynamic structures and behaviour.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Sept 2004Ron McFadyen Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Sept Ron McFadyen Sequence Diagram Objects are represented horizontally across the top of the diagram The first object is typically an Actor,
Fall 2007ACS Ron McFadyen1 Composite Pattern (see pages ) A composite is a group of objects in which some objects contain others; one object.
Sept Ron McFadyen Interaction Diagrams - Chapter 15 Describe/illustrate sequence of message exchanges among objects that are working together.
Essentials of interaction diagrams Lecture Outline Collaborations Interaction on collaboration diagrams Sequence diagrams Messages from an object.
Fall 2009ACS Ron McFadyen1 The context maintains an instance of a concrete state subclass State Pattern Each subclass (concrete state) implements.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Oct Ron McFadyen Visibility Visibility: the ability of one object to see or have a reference to another object. e.g. When a register object.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
March R McFadyen1 Figure 30.2 Layers in NextGen They only have three layers in this architecture Each layer is shown as a UML Package No separate.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
BCS 2143 Introduction to Object Oriented and Software Development.
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
Object-Oriented Design
Inspire students to develop ideas & organize thinking
Introduction to Sequence Diagrams
HTML: Tables & Frames Internet Technology1. HTML: Tables Table tags ► surround the entire table ► header row (text is boldfaced) ► surround each row ►
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Unified Modeling Language, Version 2.0
Introduction to MS WORD.
USE CASE Bayu Adhi Tama, MTI Faculty of Computer Science, University of Sriwijaya Slides are adapted from Petrus Mursanto
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 9: Interaction.
Copyright 2002 Prentice-Hall, Inc. Chapter 2 Object-Oriented Analysis and Design Modern Systems Analysis and Design Third Edition Jeffrey A. Hoffer Joey.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Decorator Pattern So many options!. Starbuzz Coffee  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the.
Group 2 work What is sequence diagram ? What is sequence diagram ? Why do we use it ? Why do we use it ? What is basic notation of SD? What is basic notation.
HTML: Tables & Frames Internet Technology.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
GoF: Document Editor Example Rebecca Miller-Webster.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Sept Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.
Interaction Diagrams Interaction Diagrams allow the designer to show how groups of objects collaborate in some behavior. –Interaction Diagrams will show.
Slide 1 Systems Analysis and Design With UML 2.0 An Object-Oriented Approach, Second Edition Chapter 2: Introduction to Object-Oriented Systems Analysis.
Microsoft Office Word 2003.
INTRODUCTORY Tutorial 5 Using CSS for Layout and Printing.
1 Unified Modeling Language, Version 2.0 Chapter 2.
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.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
CS 210 Introduction to Design Patterns September 14 th, 2006.
Microsoft Office Word 2003.
Communication DIAGRAM
Decorator Pattern Intent
Composite Pattern Context:
Communication DIAGRAM
Decorator Pattern.
Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object.
Presentation transcript:

Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern gives a mechanism without using inheritance … we can extend the capabilities of a class by adding subclasses with new methods or with different implementations of methods. The Decorator pattern allows one to add and remove layers to a base object.

Winter 2011ACS-3913 Ron McFadyen2 Decorator For example, we may have a text object and we want to sometimes add a scrollbar and sometimes we want to add a border. For example, we may have a text object and we want to sometimes add a vertical scrollbar and Sometimes we want to add a horizontal scrollbar. Original text object Scrollbar decorator Border decorator For example, we may have a text object and we want to sometimes add a vertical scrollbar and Sometimes we want to add a horizontal scrollbar.

Winter 2011ACS-3913 Ron McFadyen3 Decorator textScrollbarBorder decorators decorated The objects are linked like a linked list or chain of objects. The last in the list is the decorated object.

Winter 2011ACS-3913 Ron McFadyen4 Decorator e.g. In a windowing environment, scrolling bars, borders, etc. could be decorators on top of the text view of a document. In this example, they are all “components” :textView:scrollBar:border component draw() When it’s necessary for the document to appear (to draw itself), the draw message would be sent to :border and then: :border would draw itself; :border would send the draw message to :scrollBar which would draw itself; :scrollBar would send the draw message to :textView.

Winter 2011ACS-3913 Ron McFadyen5 Decorator If draw is sent to :border, as discussed on previous slide, what is the sequence diagram? :textView:scrollBar:border component draw()

Winter 2011ACS-3913 Ron McFadyen6 Decorator 1 component decorator Concrete component Concrete decoratorA Concrete decoratorB operation() Client See page 91

Winter 2011ACS-3913 Ron McFadyen7 Decorator 1 component Beverage Condiment Decorator HouseBlend cost() getDescription getDescription() See page 92 DarkRoast Decaf cost() Espresso cost() MilkMochaSoyWhip cost() getDescription() cost() getDescription() cost() getDescription() cost() getDescription() The text shows an attribute in these concrete decorators – an implementation needs a reference to the next component Question: What is the object diagram for a whipped mocha decaf?

Winter 2011ACS-3913 Ron McFadyen8 Decorator Beverage Condiment Decorator HouseBlend DarkRoast Decaf Espresso MilkMochaSoyWhip Decorator Concrete decorator Concrete component Component Concrete decorator Concrete component The class diagram augmented to show the roles the classes/objects play in the decorator collaboration

Winter 2011ACS-3913 Ron McFadyen9 Constructing a drink

Winter 2011ACS-3913 Ron McFadyen10 Drink Order 2

Winter 2011ACS-3913 Ron McFadyen11 Decorator Pattern - example Consider the POS system. Suppose this system must produce a sales receipt. A sales receipt will have a header and a footer, and perhaps more than one header … and more than one footer. Let’s assume the print() method of Receipt results in the receipt’s lines being printed Suppose we add coupons to the sales receipt … perhaps based on the products purchased / the season / information about the customer / etc. Line item 1 Line item 2 Line item 3 … Time of day header Product2 coupon header Money saved footer

Winter 2011ACS-3913 Ron McFadyen12 Decorator Pattern - example UML class diagram DecoratedReceipt print() receipt print() Decorator print() 1 sale timeOfDayproductCouponmoneySaved All classes, in this application of the decorator pattern, implement the print method

Winter 2011ACS-3913 Ron McFadyen13 Decorator Pattern – example object diagram a sale object is related to a receipt, but the receipt is decorated with headers and footers (as a particular receipt requires) h2:productCoupon h1: timeOfDays:sale r: receipt decorators f1: moneySaved

Winter 2011ACS-3913 Ron McFadyen14 Decorator Pattern - example Printing the receipt s:saleh1: timeOfDayh2:productCouponf1: moneySavedr: receipt print() printNote() printCoupon() printTime() print() …