装饰者模式:
首先定义抽象的组件类,子类继承该类后持有抽象类的引用,在抽象类的基础上可以在调用父类方法前后分别执行自己的行为。从而实现装饰添加新的功能特性。再次强调了使用组合的好处。
强调对扩展开放,对修改关闭。
在Java I/O中应用了装饰者模式。
装饰者模式类图:
以下程序来自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(); }
2.实现三种不同种类的饮料
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.定义添加了调料的饮料抽象类
package net.dp.decorator; public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }
4.实现不同调料的饮料
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()); } }
可以看到不同种类的饮料得出了不同的价格。