标签:输出 关闭 override 设计 decorator nbsp 抽象 end 代码
本文地址: 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
对扩展开发, 对改动关闭.
设计模式 - 装饰者模式(Decorator Pattern) 具体解释
标签:输出 关闭 override 设计 decorator nbsp 抽象 end 代码
原文地址:http://www.cnblogs.com/cxchanpin/p/6884114.html