decorator pattern so many options!. starbuzz coffee want to offer a variety of combinations of...
TRANSCRIPT
![Page 1: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/1.jpg)
Decorator Pattern
So many options!So many options!
![Page 2: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/2.jpg)
Starbuzz CoffeeStarbuzz 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
![Page 3: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/3.jpg)
First DesignFirst Design
Make a beverage class and a subclass for each legal combination
Make a beverage class and a subclass for each legal combination
![Page 4: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/4.jpg)
HouseBlendcost()
HouseBlendcost()
DarkRoastcost()
DarkRoastcost()
Decafcost()Decafcost()
Espressocost()
Espressocost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
But where are the fancy coffees?I could make this at home!
![Page 5: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/5.jpg)
HouseBlendcost()
HouseBlendcost()
DarkRoastcost()
DarkRoastcost()
Decafcost()Decafcost()
Espressocost()
Espressocost()
HouseBlendWithSteamedMilk
cost()
HouseBlendWithSteamedMilk
cost()
DarkRoastWithSteamedMilk
cost()
DarkRoastWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
![Page 6: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/6.jpg)
HouseBlendcost()
HouseBlendcost()
DarkRoastcost()
DarkRoastcost()
Decafcost()Decafcost()
Espressocost()
Espressocost()
HouseBlendWithSteamedMilk
cost()
HouseBlendWithSteamedMilk
cost()
DarkRoastWithSteamedMilk
cost()
DarkRoastWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()HouseBlendMochacost()
HouseBlendMochacost()
DarkRoastMochacost()
DarkRoastMochacost()
DecafMochacost()
DecafMochacost()
EspressoMochacost()
EspressoMochacost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
![Page 7: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/7.jpg)
HouseBlendcost()
HouseBlendcost()
DarkRoastcost()
DarkRoastcost()
Decafcost()Decafcost()
Espressocost()
Espressocost()
HouseBlendWithSteamedMilk
cost()
HouseBlendWithSteamedMilk
cost()
DarkRoastWithSteamedMilk
cost()
DarkRoastWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()HouseBlendMochacost()
HouseBlendMochacost()
DarkRoastMochacost()
DarkRoastMochacost()
DecafMochacost()
DecafMochacost()
EspressoMochacost()
EspressoMochacost()HouseBlend
WithWhipcost()
HouseBlendWithWhip
cost()
DarkRoastWithWhip
cost()
DarkRoastWithWhip
cost()
DecafWithWhip
cost()
DecafWithWhip
cost()
EspressoWithWhip
cost()
EspressoWithWhip
cost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
![Page 8: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/8.jpg)
Second DesignSecond 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?
![Page 9: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/9.jpg)
BeverageString description
milksoy
mochawhip
getDescription()<<abstract>>cost()
hasMilk()setMilk()hasSoy()setSoy()
hasMocha()setMocha()hasWhip()setWhip()
…
HouseBlendcost()
HouseBlendcost()
DarkRoastcost()
DarkRoastcost()
Decafcost()Decafcost()
Espressocost()
Espressocost()
<<abstract>>
![Page 10: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/10.jpg)
Design PrincipleDesign 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!)
![Page 11: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/11.jpg)
Decorator PatternDecorator Pattern
Start with an instance of the “basic”classes and then decorate it with new capabilities
Start with an instance of the “basic”classes and then decorate it with new capabilities
Dark Roast
Mocha
cost()cost()
Whip
cost() 0.990.99+0.200.99+0.20+0.10
![Page 12: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/12.jpg)
Key PointsKey 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
![Page 13: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/13.jpg)
Decorator PatternDecorator Pattern
![Page 14: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/14.jpg)
<<interface/abstract>>ComponentmethodA()
ConcreteComponentmethodA()
ConcreteComponentmethodA()
ConcreteDecoratorAComponent wrappedObj
methodA()newBehavior()
ConcreteDecoratorAComponent wrappedObj
methodA()newBehavior()
<<abstract>>DecoratormethodA()
<<abstract>>DecoratormethodA()
ConcreteDecoratorBComponent wrappedObj
Object newState()methodA()
ConcreteDecoratorBComponent wrappedObj
Object newState()methodA()
HAS-A
![Page 15: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/15.jpg)
Let’s Look at the CodeLet’s Look at the Code
![Page 16: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/16.jpg)
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; }}
![Page 17: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/17.jpg)
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(); }}
![Page 18: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/18.jpg)
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()); }
![Page 19: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/19.jpg)
Decorators in JavaDecorators in Java
File I/O Example File I/O Example
FileInputStream
BufferedInputStreamLineNumberInputStream
Component
Concrete decorators
![Page 20: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/20.jpg)
InputStreamInputStream
FileInputStreamFileInputStream StringBufferInputStreamStringBufferInputStream FilterInputStreamFilterInputStream
BufferedInputStreamBufferedInputStream LineNumberInputStreamLineNumberInputStream
concrete componentsconcrete components
abstract decoratorabstract decorator
concrete decoratorsconcrete decorators
abstract componentabstract component
![Page 21: 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](https://reader034.vdocument.in/reader034/viewer/2022051517/56649ee95503460f94bfb145/html5/thumbnails/21.jpg)
Lab Set UpLab Set Up
Using the Decorator Pattern to customize weapons
Using the Decorator Pattern to customize weapons