装饰者模式(decorate pattern)(咖啡)

 

定义:动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。

 

笔记:层层叠加,易于扩展。

 

 

抽象基类,饮料

package com.tj.decorator; public abstract class Beverage { protected String description = "Unknown Beverage"; public String getDescription() { return description; } public abstract double cost(); }  

 

饮料(被装饰者)实现类1,深焙咖啡

/** * */ package com.tj.decorator.coffee; import com.tj.decorator.Beverage; /** * @author 2172980000502 * */ public class DarkRoast extends Beverage { public DarkRoast() { description = "DarkRoast"; } /** * @see com.tj.decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.99; } }  

 

饮料(被装饰者)实现类2,混合咖啡

/** * */ package com.tj.decorator.coffee; import com.tj.decorator.Beverage; /** * @author 2172980000502 * */ public class HouseBlend extends Beverage { public HouseBlend() { description = "House Blend Coffee"; } /** * @see com.tj.decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.89; } }  

 

饮料(被装饰者)实现类3,浓缩咖啡

/** * */ package com.tj.decorator.coffee; import com.tj.decorator.Beverage; /** * @author 2172980000502 * */ public class Espresso extends Beverage { public Espresso() { description = "Espresso"; } /** * @see com.tj.decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 1.99; } }  

 

调料(装饰者)基类

package com.tj.decorator; public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }  

 

调料(装饰者)实现类1,摩卡

/** * */ package com.tj.decorator.flovoring; import com.tj.decorator.Beverage; import com.tj.decorator.CondimentDecorator; /** * @author 2172980000502 * */ public class Mocha extends CondimentDecorator { Beverage beverage; public Mocha(Beverage beverage) { this.beverage = beverage; } /** * @see com.tj.decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Mocha"; } /** * @see com.tj.decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.20 + beverage.cost(); } }  

 

调料(装饰者)实现类2,豆浆

/** * */ package com.tj.decorator.flovoring; import com.tj.decorator.Beverage; import com.tj.decorator.CondimentDecorator; /** * @author 2172980000502 * */ public class Soy extends CondimentDecorator { Beverage beverage; public Soy(Beverage beverage) { this.beverage = beverage; } /** * @see com.tj.decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Soy"; } /** * @see com.tj.decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.15 + beverage.cost(); } }  

 

调料(装饰者)实现类3,奶泡

/** * */ package com.tj.decorator.flovoring; import com.tj.decorator.Beverage; import com.tj.decorator.CondimentDecorator; /** * @author 2172980000502 * */ public class Whip extends CondimentDecorator { Beverage beverage; public Whip(Beverage beverage) { this.beverage = beverage; } /** * @see com.tj.decorator.CondimentDecorator#getDescription() */ @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + ", Whip"; } /** * @see com.tj.decorator.Beverage#cost() */ @Override public double cost() { // TODO Auto-generated method stub return 0.10 + beverage.cost(); } }  

 

应用入口

package com.tj.decorator; import com.tj.decorator.flovoring.*; import com.tj.decorator.coffee.*; 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(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); System.out .println(beverage3.getDescription() + " $" + beverage3.cost()); } }  

你可能感兴趣的:(head,first,设计模式)