Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 313 – Advanced Programming Topics. Decorator Pattern Intent.

Similar presentations


Presentation on theme: "CSC 313 – Advanced Programming Topics. Decorator Pattern Intent."— Presentation transcript:

1 CSC 313 – Advanced Programming Topics

2 Decorator Pattern Intent

3

4 Pizza Decorator Pattern Visual

5

6 Decorator Pattern Creation Beverage joe = new HouseBlend(); 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

11

12

13

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(); joe

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

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

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

20 Decorator Pattern Usage Beverage joe = new HouseBlend(); joe = new Mocha(joe); joe = new Whip(joe); joe = new Mocha(joe); joe bev

21 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

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 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?


Download ppt "CSC 313 – Advanced Programming Topics. Decorator Pattern Intent."

Similar presentations


Ads by Google