CSC 313 – Advanced Programming Topics
Decorator Pattern Intent
Pizza Decorator Pattern Visual
Decorator Pattern Creation Beverage joe = new HouseBlend(); 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
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(); joe
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe bev
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe bev
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); joe bev
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); joe = new Mocha(joe); joe bev
Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); joe = new Mocha(joe); int mortgage = joe.cost(); joe bev
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 but 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?