240 likes | 364 Views
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
E N D
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 getDescription() <<abstract>> cost() … Espresso cost() Decaf cost() HouseBlend cost() DarkRoast cost() But where are the fancy coffees? I could make this at home!
<<abstract>> Beverage String description getDescription() <<abstract>> cost() … Espresso cost() Decaf cost() HouseBlend cost() DarkRoast cost() EspressoWith SteamedMilk cost() DecafWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost()
<<abstract>> Beverage String description getDescription() <<abstract>> cost() … Espresso cost() Decaf cost() HouseBlend cost() DarkRoast cost() EspressoWith SteamedMilk cost() DecafWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() Espresso Mocha cost() Decaf Mocha cost() HouseBlend Mocha cost() DarkRoast Mocha cost()
<<abstract>> Beverage String description getDescription() <<abstract>> cost() … Espresso cost() Decaf cost() HouseBlend cost() DarkRoast cost() EspressoWith SteamedMilk cost() DecafWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() Espresso Mocha cost() Decaf Mocha cost() HouseBlend Mocha cost() DarkRoast Mocha cost() Espresso WithWhip cost() Decaf WithWhip cost() HouseBlend WithWhip cost() DarkRoast WithWhip 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>> Beverage String description milk soy mocha whip getDescription() <<abstract>>cost() hasMilk() setMilk() hasSoy() setSoy() hasMocha() setMocha() hasWhip() setWhip() … Decaf cost() HouseBlend cost() Espresso cost() DarkRoast 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!)
0.99 0.99+0.20 0.99+0.20+0.10 Decorator Pattern • Start with an instance of the “basic”classes and then decorate it with new capabilities Whip Mocha Dark Roast cost() cost() cost()
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
<<interface/abstract>> Component methodA() HAS-A <<abstract>> Decorator methodA() ConcreteComponent methodA() ConcreteDecoratorA Component wrappedObj methodA() newBehavior() ConcreteDecoratorB Component wrappedObj Object newState() methodA()
public abstract class Beverage{ String description = “Unknown”; public String getDescription() { return description; } public abstract double cost(); } public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); public abstract double cost(); } public class Espresso extends Beverage { public Espresso() { description = “Espresso”; } public double cost() { return 1.99; } }
public class Mocha extends CondimentDecorator { Beverage bev; public Mocha(Beverage bev) { this.bev = bev; } public String getDescription { return bev.getDescription() + “, Mocha”; } public double cost() { return .20 + bev.cost(); } }
public class StarBuzz { public static void main(String args[]) { Beverage bev = new Espresso); bev = new Mocha(bev); bev = new Mocha(bev); bev = new Whip(bev); bev = new Soy(bev); System.out.println(bev.getDescription() + “ $”+ bev.getCost()); }
Concrete decorators Component BufferedInputStream LineNumberInputStream FileInputStream Decorators in Java • File I/O Example
abstract component InputStream abstract decorator FileInputStream FilterInputStream StringBufferInputStream BufferedInputStream LineNumberInputStream concrete components concrete decorators
Lab Set Up • Using the Decorator Pattern to customize weapons