1 / 21

Decorator Pattern

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

lucius
Download Presentation

Decorator Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  4. <<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!

  5. <<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()

  6. <<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()

  7. <<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()

  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?

  9. <<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()

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

  11. 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()

  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

  13. Decorator Pattern

  14. <<interface/abstract>> Component methodA() HAS-A <<abstract>> Decorator methodA() ConcreteComponent methodA() ConcreteDecoratorA Component wrappedObj methodA() newBehavior() ConcreteDecoratorB Component wrappedObj Object newState() methodA()

  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 Mocha(bev); bev = new Whip(bev); bev = new Soy(bev); System.out.println(bev.getDescription() + “ $”+ bev.getCost()); }

  19. Concrete decorators Component BufferedInputStream LineNumberInputStream FileInputStream Decorators in Java • File I/O Example

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

  21. Lab Set Up • Using the Decorator Pattern to customize weapons

More Related