1 / 24

Lecture 12: Implementing Decorators

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

geri
Download Presentation

Lecture 12: Implementing Decorators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 313 – Advanced Programming Topics Lecture 12:Implementing Decorators

  2. 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

  3. 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)

  4. Decorator Pattern Visual Pizza

  5. Decorator Pattern Visual Anchovy Garlic Olives Pizza

  6. 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();

  7. Decorators’ Dirty Secrets • Decorators are subclasses of main class

  8. Decorators’ Dirty Secrets • Decorators are subclasses of main class

  9. Decorators’ Dirty Secrets • Decorators are subclasses of main class • Almost recursive

  10. Meet the Decorator Classes • AbstractComponent • Component & decoratorsuperclass • Only type used outsidepattern • Interface or abstract class • Defines methods calledby code outside pattern

  11. 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

  12. Meet the Decorator Classes • AbstractDecorator • Decorator superclass • Declares component field • Abstract class only • Re-declares allinherited methodsto make abstract

  13. Meet the Decorator Classes • ConcreteDecorator(s) • Add fields or functionalityto ConcreteComponent • All methods definedso not abstract • Calls methodin component field • May instantiate 0 to ∞

  14. Decorator Pattern Usage Drink martini = new Gin(); martini = new Vermouth(martini); martini = new Ice(martini); martini = martini.shake();

  15. Decorator Pattern Usage Drink martini = new Gin(); martini = new Vermouth(martini); martini = new Ice(martini); martini = martini.shake(); = martini.pour();

  16. Decorator Pattern Usage Beverage joe = new HouseBlend(); HBlend joe

  17. Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); Mocha bev HBlend joe

  18. Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); Mocha bev HBlend joe

  19. Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); Whip Mocha bev HBlend bev joe

  20. 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

  21. 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

  22. Decorator Example

  23. 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

  24. For Next Lecture • Lab #3 available on Angel • Asks you to implement Decorator Pattern • Have time Friday, butmay 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?

More Related