Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Decorator Pattern So many options!

2 Starbuzz Coffee  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the combination that was ordered  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the combination that was ordered

3 First Design  Make a beverage class and a subclass for each legal combination

4 HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() Beverage String description getDescription() > cost() … > But where are the fancy coffees? I could make this at home!

5 HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() HouseBlendWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DecafWith SteamedMilk cost() DecafWith SteamedMilk cost() EspressoWith SteamedMilk cost() EspressoWith SteamedMilk cost() Beverage String description getDescription() > cost() … >

6 HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() HouseBlendWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DecafWith SteamedMilk cost() DecafWith SteamedMilk cost() EspressoWith SteamedMilk cost() EspressoWith SteamedMilk cost() HouseBlend Mocha cost() HouseBlend Mocha cost() DarkRoast Mocha cost() DarkRoast Mocha cost() Decaf Mocha cost() Decaf Mocha cost() Espresso Mocha cost() Espresso Mocha cost() Beverage String description getDescription() > cost() … >

7 HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() HouseBlendWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DecafWith SteamedMilk cost() DecafWith SteamedMilk cost() EspressoWith SteamedMilk cost() EspressoWith SteamedMilk cost() HouseBlend Mocha cost() HouseBlend Mocha cost() DarkRoast Mocha cost() DarkRoast Mocha cost() Decaf Mocha cost() Decaf Mocha cost() Espresso Mocha cost() Espresso Mocha cost() HouseBlend WithWhip cost() HouseBlend WithWhip cost() DarkRoast WithWhip cost() DarkRoast WithWhip cost() Decaf WithWhip cost() Decaf WithWhip cost() Espresso WithWhip cost() Espresso WithWhip cost() Beverage String description getDescription() > cost() … >

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

9 Beverage String description milk soy mocha whip getDescription() >cost() hasMilk() setMilk() hasSoy() setSoy() hasMocha() setMocha() hasWhip() setWhip() … HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() >

10 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!)  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!)

11 Decorator Pattern  Start with an instance of the “basic”classes and then decorate it with new capabilities Dark Roast Mocha cost() Whip cost() 0.99 0.99+0.20 0.99+0.20+0.10

12 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  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

13 Decorator Pattern

14 > Component methodA() ConcreteComponent methodA() ConcreteComponent methodA() ConcreteDecoratorA Component wrappedObj methodA() newBehavior() ConcreteDecoratorA Component wrappedObj methodA() newBehavior() > Decorator methodA() > Decorator methodA() ConcreteDecoratorB Component wrappedObj Object newState() methodA() ConcreteDecoratorB Component wrappedObj Object newState() methodA() HAS-A

15 Let’s Look at the Code

16 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; }

17 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(); }

18 public class StarBuzz { 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.getDescription() + “ $”+ bev.getCost()); }

19 Decorators in Java  File I/O Example FileInputStreamBufferedInputStream LineNumberInputStream Component Concrete decorators

20 InputStream FileInputStream StringBufferInputStream FilterInputStream BufferedInputStream LineNumberInputStream concrete components abstract decorator concrete decorators abstract component

21 Lab Set Up  Using the Decorator Pattern to customize weapons


Download ppt "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."

Similar presentations


Ads by Google