jan 200692.3913 ron mcfadyen1 decorator sometimes we need a way to add responsibilities to an object...

6
Jan 2006 92.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.

Upload: shannon-hutchinson

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jan 200692.3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern

Jan 2006 92.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: Jan 200692.3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern

Jan 2006 92.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: Jan 200692.3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern

Jan 2006 92.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: Jan 200692.3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern

Jan 2006 92.3913 Ron McFadyen 4

Decorator

1 componentcomponent

decoratorConcrete component

Concrete decoratorA

Concrete decoratorB

operation()

operation()

operation() operation()

operation()

Client

See page 91

Page 5: Jan 200692.3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern

Jan 2006 92.3913 Ron McFadyen 5

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 6: Jan 200692.3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern

Jan 2006 92.3913 Ron McFadyen 6

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