标签:设计模式
装饰者模式:装饰者模式类图:
以下程序来自Head First设计模式中的实例代码,模拟了不同种类的饮料不同的售价计算方法。
1.定义饮料接口
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 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; } }
package net.dp.decorator; public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }
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.终于大功告成!跑一把!
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()); } }
标签:设计模式
原文地址:http://blog.csdn.net/liuchangqing123/article/details/43938445