Download presentation
Presentation is loading. Please wait.
Published byShannon Hutchinson Modified over 9 years ago
1
Jan 200692.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.
2
Jan 200692.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.
3
Jan 200692.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.
4
Jan 200692.3913 Ron McFadyen4 Decorator 1 component decorator Concrete component Concrete decoratorA Concrete decoratorB operation() Client See page 91
5
Jan 200692.3913 Ron McFadyen5 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?
6
Jan 200692.3913 Ron McFadyen6 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.