Decorator Pattern So many options!
Feb 25, 2016
Decorator PatternSo 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
HouseBlendcost()
DarkRoastcost()
Decafcost()
Espressocost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
But where are the fancy coffees?I could make this at home!
HouseBlendcost()
DarkRoastcost()
Decafcost()
Espressocost()HouseBlendWith
SteamedMilkcost()
DarkRoastWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
HouseBlendcost()
DarkRoastcost()
Decafcost()
Espressocost()HouseBlendWith
SteamedMilkcost()
DarkRoastWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()HouseBlendMochacost()
DarkRoastMochacost()
DecafMochacost()
EspressoMochacost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
HouseBlendcost()
DarkRoastcost()
Decafcost()
Espressocost()HouseBlendWith
SteamedMilkcost()
DarkRoastWithSteamedMilk
cost()
DecafWithSteamedMilk
cost()
EspressoWithSteamedMilk
cost()HouseBlendMochacost()
DarkRoastMochacost()
DecafMochacost()
EspressoMochacost()HouseBlend
WithWhipcost()
DarkRoastWithWhip
cost()
DecafWithWhip
cost()
EspressoWithWhip
cost()
BeverageString descriptiongetDescription()
<<abstract>> cost()…
<<abstract>>
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?
BeverageString description
milksoy
mochawhip
getDescription()<<abstract>>cost()
hasMilk()setMilk()hasSoy()setSoy()
hasMocha()setMocha()hasWhip()setWhip()
…
HouseBlendcost()
DarkRoastcost()
Decafcost()
Espressocost()
<<abstract>>
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
Dark Roast
Mocha
cost()cost()
Whip
cost() 0.990.99+0.200.99+0.20+0.10
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>>ComponentmethodA()
ConcreteComponentmethodA()
ConcreteDecoratorAComponent wrappedObj
methodA()newBehavior()
<<abstract>>DecoratormethodA()
ConcreteDecoratorBComponent wrappedObj
Object newState()methodA()
HAS-A
Let’s Look at the Code
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()); }
Decorators in Java File I/O Example
FileInputStream
BufferedInputStreamLineNumberInputStream
Component
Concrete decorators
InputStream
FileInputStream StringBufferInputStream FilterInputStream
BufferedInputStream LineNumberInputStream
concrete components
abstract decorator
concrete decorators
abstract component
Lab Set Up Using the Decorator Pattern to
customize weapons