winter 2011acs-3913 ron mcfadyen1 decorator sometimes we need a way to add responsibilities to an...

14
Winter 2011 ACS-3913 Ron McFadyen 1 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.

Post on 22-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

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

Winter 2011 ACS-3913 Ron McFadyen 1

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.

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

Winter 2011 ACS-3913 Ron McFadyen 2

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 ex

ampl

e, we m

ay ha

ve

a tex

t obj

ect a

nd w

e wan

t

to so

meti

mes

add a

verti

cal s

crol

lbar

and

Someti

mes

we w

ant t

o add

a

horiz

ontal

scro

llbar

.

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.

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

Winter 2011 ACS-3913 Ron McFadyen 3

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.

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

Winter 2011 ACS-3913 Ron McFadyen 4

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 component

draw()draw()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.

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

Winter 2011 ACS-3913 Ron McFadyen 5

Decorator

If draw is sent to :border , as discussed on previous slide, what is the sequence diagram?

:textView:scrollBar:border

component component

draw()draw()draw()

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

Winter 2011 ACS-3913 Ron McFadyen 6

Decorator

1 componentcomponent

decoratorConcrete component

Concrete decoratorA

Concrete decoratorB

operation()

operation()

operation() operation()

operation()

Client

See page 91

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

Winter 2011 ACS-3913 Ron McFadyen 7

Decorator

1 componentBeverage

Condiment DecoratorHouseBlend

cost()

getDescription

getDescription()

See page 92

DarkRoast

Decaf

cost()

cost()

cost()

Espressocost()

Milk Mocha Soy Whip 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?

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

Winter 2011 ACS-3913 Ron McFadyen 8

Decorator

Beverage

Condiment DecoratorHouseBlend DarkRoast

DecafEspresso

Milk Mocha Soy Whip

DecoratorDecorator

Concrete decorator

Concrete component

Component

Concrete decorator

Concrete decorator

Concrete decorator

Concrete component

Concrete component

Concrete component

The class diagram augmented to show the roles the classes/objects play in the decorator collaboration

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

Winter 2011 ACS-3913 Ron McFadyen 9

Constructing a drink

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

Winter 2011 ACS-3913 Ron McFadyen 10

Drink Order 2

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

Winter 2011 ACS-3913 Ron McFadyen 11

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 1Line item 2Line item 3…

Time of day header

Product2 coupon header

Money saved footer

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

Winter 2011 ACS-3913 Ron McFadyen 12

Decorator Pattern - example

UML class diagram

DecoratedReceipt

print()

receipt

print()

Decorator

print()

1sale

timeOfDay productCoupon moneySaved

All classes, in this application of the decorator pattern, implement the print method

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

Winter 2011 ACS-3913 Ron McFadyen 13

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

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

Winter 2011 ACS-3913 Ron McFadyen 14

Decorator Pattern - example Printing the receipt

s:sale h1: timeOfDay h2:productCoupon f1: moneySaved r: receipt

print()

print()

print()

print()

printNote()

printCoupon()

printTime()

print()