Design Pattern-decorator pattern
Decoration mode:
First, define the abstract component class. The subclass inherits the class and holds the reference of the abstract class. On the basis of the abstract class, you can execute your own behavior before and after calling the parent class method. To add new features for decoration. The advantages of combination are emphasized again.
Emphasize opening to extensions and disabling modifications.
The modifier mode is applied to Java I/O.
Decoration mode class diagram:
The following program comes from the instance code in Head First design mode, simulating the calculation method of different prices for different types of drinks.
1. Define the beverage Interface
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> package net. dp. decorator; public abstract class Beverage {protected String description = "Unknown Beverage"; public String getDescription () {return description;} public abstract double cost ();}
2. Three different types of drinks
package net.dp.decorator.coffee; import net.dp.decorator.Beverage; public class Espresso extends Beverage { public Espresso() { description = "Espresso"; } public double cost() { return 1.99; }}
package net.dp.decorator; public abstract class Beverage { protected String description = "Unknown Beverage"; public String getDescription() { return description; } public abstract double cost();}
package net.dp.decorator.coffee; import net.dp.decorator.Beverage; public class HouseBlend extends Beverage { public HouseBlend() { description = "House Blend Coffee"; } public double cost() { return .89; }}
3. Define the beverage abstract class with spices added
package net.dp.decorator; public abstract class CondimentDecorator extends Beverage { public abstract String getDescription();}
4. Implement drinks with different spices
package net.dp.decorator.flovoring; import net.dp.decorator.Beverage;import net.dp.decorator.CondimentDecorator; 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(); }}
package net.dp.decorator.flovoring; import net.dp.decorator.Beverage;import net.dp.decorator.CondimentDecorator; 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(); }}
package net.dp.decorator.flovoring; import net.dp.decorator.Beverage;import net.dp.decorator.CondimentDecorator; 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(); }}5. The final success! Run it!
package net.dp.decorator; import net.dp.decorator.coffee.DarkRoast;import net.dp.decorator.coffee.Espresso;import net.dp.decorator.coffee.HouseBlend;import net.dp.decorator.flovoring.Mocha;import net.dp.decorator.flovoring.Soy;import net.dp.decorator.flovoring.Whip; 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(beverage2); beverage3 = new Mocha(beverage2); beverage3 = new Whip(beverage2); System.out .println(beverage3.getDescription() + " $" + beverage3.cost()); }}
We can see that different types of drinks have different prices.