SE 2811 Midterm Scores 100 90 80 70

  • Slides: 16
Download presentation
SE 2811 Midterm Scores 100 90 80 70 60 50 40 30 20 10

SE 2811 Midterm Scores 100 90 80 70 60 50 40 30 20 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 SE-2811 Dr. Mark L. Hornick 1

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

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

Example: The “Ice Cream Store” Ice Cream #description What kind of a class is

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

Decorator Pattern Intent You want to attach additional functionality to an (existing) class dynamically…

Decorator Pattern Intent You want to attach additional functionality to an (existing) class dynamically… l …without having to resort to sub-classing the existing class § l 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

The “Ice Cream Store” (contd. ) public abstract class Ice. Cream { String description;

The “Ice Cream Store” (contd. ) public abstract class Ice. Cream { String description; public String get. Description() { return description; } Ice Cream description public abstract double cost(); get. Description() cost() } public final class Cone extends Ice. Cream { Dish Cone cost() public Cone() { description = “Cone”; } public double cost() { return 1. 24; } cost() } 5

Extending functionality l Now in addition to your ice cream, you can also ask

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

Alternative 1: Create a new class for each combination. Ice Cream Toppings Cone (abstract)

Alternative 1: Create a new class for each combination. Ice Cream Toppings Cone (abstract) M&M Dish Fudge Waffle. Cone Peanuts Caramel Any problems with this approach?

Alternative 1: Create a new class for each combination. Ice Cream Toppings Cone M&M

Alternative 1: Create a new class for each combination. Ice Cream Toppings Cone M&M Dish Fudge Waffle. Cone Peanuts 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? • What if a customer wants a combination of toppings?

Alternative 2: Using instance variables to keep track of the toppings? Ice. Cream -description:

Alternative 2: Using instance variables to keep track of the toppings? Ice. Cream -description: String -has. Fudge: boolean -has. Mn. M: boolean ------+get. Description() +cost() +has. Fudge() +has. Caramel() -----9

Alternative 2 (contd. ) public abstract class Ice. Cream { ……………… public double cost()

Alternative 2 (contd. ) public abstract class Ice. Cream { ……………… public double cost() { double topping. Cost = 0; if (has. Fudge()) { topping. Cost += oreo. Cost; } if (has. Caramel()) { topping. Cost += caramel. Cost; } …. return topping. Cost; } } 10

Alternative 2 (contd. ) public class Cone extends Ice. Cream { ……………… public double

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

So what’s the problem? l l We want to allow existing classes to be

So what’s the problem? l l 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!!

SE-2811 Dr. Mark L. Hornick 13

SE-2811 Dr. Mark L. Hornick 13

SE-2811 Dr. Mark L. Hornick 14

SE-2811 Dr. Mark L. Hornick 14

Using the Decorator Pattern l Take the Ice. Cream object l l l Decorate

Using the Decorator Pattern l Take the Ice. Cream object l l l Decorate it with the Fudge topping. l l l Create a Fudge object and wrap it around Cone. cone = new Fudge(); Decorate it with the Nuts topping. l l l Let’s create the Cone object. Ice. Cream cone= new Cone(); 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. l l Call cost on the outmost decorator. System. out. println(cone. cost());

Summary l Decorators have the same super-type as the objects they decorate. l One

Summary l Decorators have the same super-type as the objects they decorate. l One or more decorators can be used to wrap an object. l 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. l The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job. l Objects can be decorated at any time, so we can decorate objects at runtime with as many decorators as we like.