本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033
装饰者模式(Decorator Pattern):动态地将责任附加到对象上. 若要扩展功能, 装饰者提供了比继承更有弹性的替代方案.
使用方法:
1. 首先创建组件(Component)父类, 所有类,具体组件(Concrete Component)和装饰者(Decorator)都属于这一类型, 可以进行扩展;
可以是抽象类(abstract class), 也可以是接口(interface);
代码:
/** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public abstract class Beverage { String description = "Unkown Beverage"; public String getDescription() { return description; } public abstract double cost(); }
可以是抽象类(abstract class), 也可以是接口(interface);
代码:
/** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }
代码:
/** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class DarkRoast extends Beverage { public DarkRoast() { description = "Dark Roast"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.99; } } /** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class Decat extends Beverage { public Decat() { description = "Decat"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 1.05; } } /** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class Espresso extends Beverage { public Espresso() { description = "Espresso"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 1.99; } } /** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class HouseBlend extends Beverage { public HouseBlend() { description = "House Blend Coffee"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return .89; } }
代码:
/** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class Milk extends CondimentDecorator { Beverage beverage; public Milk(Beverage beverage) { this.beverage = beverage; } /* (non-Javadoc) * @see decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Milk"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.10 + beverage.cost(); } } /** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class Mocha extends CondimentDecorator { Beverage beverage; public Mocha(Beverage beverage) { this.beverage = beverage; } /* (non-Javadoc) * @see decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Mocha"; } @Override public double cost() { // TODO Auto-generated method stub return 0.20+beverage.cost(); } } /** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class Soy extends CondimentDecorator { Beverage beverage; public Soy(Beverage beverage) { this.beverage = beverage; } /* (non-Javadoc) * @see decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Soy"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.15 + beverage.cost(); } } /** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class Whip extends CondimentDecorator { Beverage beverage; public Whip(Beverage beverage) { this.beverage = beverage; } /* (non-Javadoc) * @see decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Whip"; } /* (non-Javadoc) * @see decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.10 + beverage.cost(); } }
代码:
/** * @time 2014年5月23日 */ package decorator; /** * @author C.L.Wang * */ public class StarbuzzCoffee { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub 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()); } }
Espresso $1.99 Dark Roast, Mocha, Mocha, Whip $1.49 House Blend Coffee, Soy, Mocha, Whip $1.34
对扩展开发, 对修改关闭.