View
58
Download
0
Category
Preview:
DESCRIPTION
CSC 313 – Advanced Programming Topics. Lecture 12: Implementing Decorators. Decorator Pattern Intent. Invisibly augment main concept instances Turn coffee into a double mocha with whip Make $2 sandwich cost $2.17 Decorators add functionality by wrapping - PowerPoint PPT Presentation
Citation preview
LECTURE 12:IMPLEMENTING DECORATORS
CSC 313 – Advanced Programming Topics
Decorator Pattern Intent
Invisibly augment main concept instances Turn coffee into a double mocha with
whip Make $2 sandwich cost $2.17
Decorators add functionality by wrapping Original instance wrapped by new
decorator Can also have decorator wrap another
decorator
Decorator Pattern Intent
Invisibly augment main concept instances Turn coffee into a double mocha with
whip Make $2 sandwich cost $2.17
Decorators add functionality by wrapping Original instance wrapped by new
decorator Can also have decorator wrap another
decorator
Violates all rules of good design (Not an actual intent, just good fun)
Pizza
Decorator Pattern Visual
Decorator Pattern Visual
Anchovy
GarlicOlives
Pizza
Decorator Pattern CreationBeverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Mocha(joe);joe = new Whip(joe);joe = new Tall(joe);int mortgage = joe.cost();
Decorators’ Dirty Secrets
Decorators are subclasses of main class
Decorators’ Dirty Secrets
Decorators are subclasses of main class
Decorators’ Dirty Secrets
Decorators are subclasses of main class
Almost recursive
Meet the Decorator Classes AbstractComponent
Component & decoratorsuperclass
Only type used outsidepattern
Interface or abstract class
Defines methods calledby code outside pattern
Meet the Decorator Classes ConcreteComponent(s)
Base concept defined here Only role always created Instance at root
of decoration Holds vital fields Basic methods defined
in this class
Meet the Decorator Classes AbstractDecorator
Decorator superclass Declares component field Abstract class only Re-declares all
inherited methodsto make abstract
Meet the Decorator Classes ConcreteDecorator(s)
Add fields or functionalityto ConcreteComponent
All methods definedso not abstract Calls method
in component field May instantiate 0 to ∞
Decorator Pattern Usage
Drink martini = new Gin();martini = new Vermouth(martini);martini = new Ice(martini);martini = martini.shake();
Decorator Pattern Usage
Drink martini = new Gin();martini = new Vermouth(martini);martini = new Ice(martini);martini = martini.shake();
= martini.pour();
Decorator Pattern Usage
Beverage joe = new HouseBlend();
HBlendjoe
Mocha
Decorator Pattern Usage
Beverage joe = new HouseBlend();new Mocha(joe);
HBlendjoe
bev
Mocha
Decorator Pattern Usage
Beverage joe = new HouseBlend();joe = new Mocha(joe);
HBlendjoe
bev
WhipMocha
Decorator Pattern Usage
Beverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Whip(joe);
HBlendjoe
bevbev
MochaWhip
Mocha
Decorator Pattern Usage
Beverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Whip(joe);joe = new Mocha(joe);
HBlendjoe
bevbevbe
v
MochaWhip
Mocha
Decorator Pattern Usage
Beverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Whip(joe);joe = new Mocha(joe);int mortgage = joe.cost();
HBlendjoe
bevbevbe
v
Decorator Example
Decorators: Good or Bad
Pros: Invisibly add to
classes Enable code reuse Limit code written Creates classes
that are closed to modification
Cons: No reality in
hierarchy Use mangled
recursion Slow, polymorphic
calls used everywhere
For Next Lecture
Lab #3 available on Angel Asks you to implement Decorator
Pattern Have time Friday, but may want help
profiling Two (short) readings available on
web Is this method hot or uglier than ____
Mom? What rules of thumb exist for where to
optimize? How to express improvement so it is
meaningful? Could we compute maximum benefit
from opts?
Recommended