Decorator mode definition: dynamically attaches a responsibility to an object. To expand the functionality, the decoration provides an alternative solution that is more flexible than continuing.
Simple definition: Wrap an object to provide new behavior.
The annotator mode can effectively deal with the explosion-like problem.
OO principles:
It is open to extensions and closed to modifications.
Take StarbuzzCoffee in the book as an example:
Beverage. java (Beverage abstract class)
package headfirst.decorator.starbuzz;public abstract class Beverage {String description = "Unknown Beverage"; public String getDescription() {return description;} public abstract double cost();}
CondimentDecorator. java (seasoning abstract class)
package headfirst.decorator.starbuzz;public abstract class CondimentDecorator extends Beverage {public abstract String getDescription();}
DarkRoast. java (beverage class)
package headfirst.decorator.starbuzz;public class DarkRoast extends Beverage {public DarkRoast() {description = "Dark Roast Coffee";} public double cost() {return .99;}}
Espresso. java
package headfirst.decorator.starbuzz;public class Espresso extends Beverage { public Espresso() {description = "Espresso";} public double cost() {return 1.99;}}
HouseBlend. java
package headfirst.decorator.starbuzz;public class HouseBlend extends Beverage {public HouseBlend() {description = "House Blend Coffee";} public double cost() {return .89;}}
Mocha. java (Specific spices class)
package headfirst.decorator.starbuzz;public class Mocha extends CondimentDecorator {Beverage beverage; public Mocha(Beverage beverage) {this.beverage = beverage;} public String getDescription() {return beverage.getDescription() + ", Mocha";} public double cost() {return .20 + beverage.cost();}}
Soy. java
package headfirst.decorator.starbuzz;public class Soy extends CondimentDecorator {Beverage beverage;public Soy(Beverage beverage) {this.beverage = beverage;}public String getDescription() {return beverage.getDescription() + ", Soy";}public double cost() {return .15 + beverage.cost();}}
Whip. java
package headfirst.decorator.starbuzz; public class Whip extends CondimentDecorator {Beverage beverage; public Whip(Beverage beverage) {this.beverage = beverage;} public String getDescription() {return beverage.getDescription() + ", Whip";} public double cost() {return .10 + beverage.cost();}}
Test class: StarbuzzCoffee. java
package headfirst.decorator.starbuzz;public class StarbuzzCoffee { public static void main(String args[]) {Beverage beverage = new Espresso();System.out.println(beverage.getDescription() + " $" + beverage.cost()); Beverage beverage2 = new DarkRoast();beverage2 = new Mocha(beverage2);beverage2 = new Mocha(beverage2);beverage2 = new Whip(beverage2);System.out.println(beverage2.getDescription() + " $" + beverage2.cost()); Beverage beverage3 = new HouseBlend();beverage3 = new Soy(beverage3);beverage3 = new Mocha(beverage3);beverage3 = new Whip(beverage3);System.out.println(beverage3.getDescription() + " $" + beverage3.cost());}}
Output:
Espresso $1.99Dark Roast Coffee, Mocha, Mocha, Whip $1.49House Blend Coffee, Soy, Mocha, Whip $1.34