Decorator Pattern So many options Starbuzz Coffee Want
Decorator Pattern So many options!
Starbuzz Coffee § Want to offer a variety of combinations of coffee and condiments § Cost of a cup depends on the combination that was ordered
First Design § Make a beverage class and a subclass for each legal combination
<<abstract>> Beverage String description get. Description() <<abstract>> cost() … House. Blend cost() Dark. Roast cost() Decaf cost() Espresso cost() But where are the fancy coffees? I could make this at home!
<<abstract>> Beverage String description get. Description() <<abstract>> cost() … House. Blend Dark. Roast cost() House. Blend. With Steamed. Milk Dark. Roast. With Steamed. Milk cost() Espresso Decaf cost() Decaf. With Espresso. With Steamed. Milk cost()
<<abstract>> Beverage String description get. Description() <<abstract>> cost() … House. Blend Dark. Roast cost() House. Blend. With Steamed. Milk Dark. Roast. With Steamed. Milk House. Blend cost() Dark. Roast cost() Mocha cost() Espresso Decaf cost() Decaf. With Espresso. With Steamed. Milk Espresso cost() Decaf cost() Mocha cost()
<<abstract>> Beverage String description get. Description() <<abstract>> cost() … Espresso Decaf House. Blend Dark. Roast cost() Decaf. With Espresso. With House. Blend. With Steamed. Milk Dark. Roast. With Steamed. Milk Espresso Steamed. Milk cost() Decaf House. Blend cost() Dark. Roast Mocha cost() Mocha cost() Espresso cost() Decaf House. Blend cost() With. Whip Dark. Roast With. Whip cost()
Second Design § Make the superclass contain booleans to specify which condiments are included and subclasses for each type of coffee § How do we compute cost? § What does it take to add a new condiment?
<<abstract>> House. Blend cost() Dark. Roast cost() Beverage String description milk soy mocha whip get. Description() <<abstract>>cost() has. Milk() set. Milk() has. Soy() set. Soy() has. Mocha() set. Mocha() has. Whip() set. Whip() … Decaf cost() Espresso cost()
Design Principle § Classes should be open for extension, but closed for modification § “extension” is NOT subclassing § It means the addition of new behavior (without modifying the code!)
Decorator Pattern § Start with an instance of the “basic”classes and then decorate it with new capabilities Whip Mocha Dark Roast cost() 0. 99+0. 20+0. 10 cost() 0. 99+0. 20 cost() 0. 99
Key Points § Decorators have the same supertypes as the objects they decorate § This lets us pass around the decorated object instead of the original (unwrapped) object § Decorator add behavior by delegating to the object it decorates and then adding its own behavior § Can add decorations at any time
Decorator Pattern
<<interface/abstract>> Component method. A() HAS-A Concrete. Component method. A() <<abstract>> Decorator method. A() Concrete. Decorator. A Component wrapped. Obj method. A() new. Behavior() Concrete. Decorator. B Component wrapped. Object new. State() method. A()
Let’s Look at the Code
public abstract class Beverage{ String description = “Unknown”; public String get. Description() { return description; } public abstract double cost(); } public abstract class Condiment. Decorator extends Beverage { public abstract String get. Description(); public abstract double cost(); } public class Espresso extends Beverage { public Espresso() { description = “Espresso”; } public double cost() { return 1. 99; } }
public class Mocha extends Condiment. Decorator { Beverage bev; public Mocha(Beverage bev) { this. bev = bev; } public String get. Description { return bev. get. Description() + “, Mocha”; } public double cost() { return. 20 + bev. cost(); } }
public class Star. Buzz { public static void main(String args[]) { Beverage bev = new Espresso); bev = new Mocha(bev); bev = new Whip(bev); bev = new Soy(bev); System. out. println(bev. get. Description() + “ $”+ bev. get. Cost()); }
Decorators in Java § File I/O Example Concrete decorators Component Buffered. Input. Stream Line. Number. Input. Stream File. Input. Stream
abstract component File. Input. Stream abstract decorator String. Buffer. Input. Stream Buffered. Input. Stream Filter. Input. Stream Line. Number. Input. Stream concrete components concrete decorators
Lab Set Up § Using the Decorator Pattern to customize weapons
- Slides: 21