Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2.

Similar presentations


Presentation on theme: "SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2."— Presentation transcript:

1 SE-2811 Dr. Mark L. Hornick 1

2 The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2

3 Example: The “Ice Cream Store” 3 Ice Cream #description +getDescription() +cost() Cone cost() Dish cost() What kind of a class is Ice Cream ? What kind of a method is cost ? Why? Subclasses define their own implementation and return the cost of the IceCream. WaffleCone cost()

4 Decorator Pattern Intent You want to attach additional functionality to an (existing) class dynamically… …without having to resort to sub-classing the existing class  It’s also possible that the existing class is declared final, in which case you can’t subclass it …or making any modifications to the existing class  Also, you may not have access to the existing class’s source

5 The “Ice Cream Store” (contd.) 5 Ice Cream description getDescription() cost() Cone cost() Dish cost() public abstract class IceCream { String description; public String getDescription() { return description; } public abstract double cost(); } public class Cone extends IceCream { public Cone() { description = “Cone”; } public double cost() { return 1.24; }

6 Extending functionality Now in addition to your ice cream, you can also ask for additional toppings (Fudge, M&M’s, peanuts, …) Charges are extra for each additional topping. How should we design the system?

7 Alternative 1: Create a new class for each combination. Ice CreamToppings ConeM&M DishFudge WaffleConePeanuts Caramel Any problems with this approach?

8 Alternative 1: Create a new class for each combination. Ice CreamToppings ConeM&M DishFudge WaffleConePeanuts Caramel Results in class explosion! Maintenance nightmare! What happens when a new topping is added? What happens when the cost of a topping (e.g. Fudge) changes?

9 Alternative 2: Using instance variables to keep track of the toppings? 9 IceCream -description: String -hasFudge: boolean -hasMnM: boolean ------- +getDescription() +cost() +hasFudge() +hasCaramel() ------

10 Alternative 2 (contd.) 10 public class Icecream { ……………… public double cost() { double toppingCost = 0; if (hasFudge()) { toppingCost += oreoCost; } if (hasCaramel()) { toppingCost += caramelCost; } …. return toppingCost; }

11 Alternative 2 (contd.) 11 public class Cone{ ……………… public double cost() { return 1.24 + super.cost(); } What is this method calculating and returning?

12 So what’s the problem? We want to allow existing classes to be easily adapted to incorporate new behavior without modifying existing code. We want a design that is flexible enough to take on new functionality to meet changing requirements. The Decorator Pattern comes to the rescue!!

13 SE-2811 Dr. Mark L. Hornick 13

14 SE-2811 Dr. Mark L. Hornick 14

15 Using the Decorator Pattern Take the IceCream object Let’s create the Cone object. IceCream cone= new Cone(); Decorate it with the Fudge topping. Create a Fudge object and wrap it around Cone. cone = new Fudge(); Decorate it with the Nuts topping. Create a Nuts object and wrap it around Cone. cone = new Nuts(cone); Call the cost method and rely on delegation to add the cost of all toppings. Call cost on the outmost decorator. System.out.println(cone.cost());

16 Summary Decorators have the same super-type as the objects they decorate. One or more decorators can be used to wrap an object. Given that the decorator has the same super-type as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object. The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job. Objects can be decorated at any time, so we can decorate objects at runtime with as many decorators as we like.


Download ppt "SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2."

Similar presentations


Ads by Google