240 likes | 397 Views
CSC 313 – Advanced Programming Topics. Lecture 11: 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
E N D
CSC 313 – Advanced Programming Topics Lecture 11:Implementing Decorators
Decorator Pattern Intent • Invisiblyaugment 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 • Invisiblyaugment 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)
Decorator Pattern Visual Pizza
Decorator Pattern Visual Anchovy Garlic Olives Pizza
Decorator Pattern Creation Beverage 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 rootof decoration • Holds vital fields • Basic methods definedin this class
Meet the Decorator Classes • AbstractDecorator • Decorator superclass • Declares component field • Abstract class only • Re-declares allinherited methodsto make abstract
Meet the Decorator Classes • ConcreteDecorator(s) • Add fields or functionalityto ConcreteComponent • All methods definedso not abstract • Callsmethodin 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(); HBlend joe
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); Mocha bev HBlend joe
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); Mocha bev HBlend joe
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); Whip Mocha bev HBlend bev joe
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); joe = new Mocha(joe); Mocha Whip Mocha bev HBlend bev bev joe
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); joe = new Mocha(joe); int mortgage = joe.cost(); Mocha Whip Mocha bev HBlend bev bev joe
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 web/Angel • Asks you to implement the Observer Pattern • Short -- due before lab next Friday • Think about recursion • What are its benefits? • What are the costs? • Can we do anything to limit these costs?