设计模式之装饰者模式
装饰者模式
1.装饰者模式就像打包一个快递
(1)主体:陶瓷,衣服。
(2)报纸填充,塑料泡沫,纸板,木板
2.Component
3.ConcreteComponent和Decorator
4.装饰者模式:动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性。
5.有些递归的意思
类结构图
示例代码
package com.coffeebar.main; public abstract class Drink { public String description = ""; private float price = 0f; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public abstract float cost(); }
package com.coffeebar.coffee; import com.coffeebar.main.Drink; public class Coffee extends Drink { @Override public float cost() { // TODO Auto-generated method stub return super.getPrice(); } }
package com.coffeebar.coffee; import com.coffeebar.main.Drink; public class Decaf extends Coffee { public Decaf() { super.setDescription("Decaf"); super.setPrice(3.0f); } }
package com.coffeebar.coffee; public class Espresso extends Coffee { public Espresso() { super.setDescription("Espresso"); super.setPrice(4.0f); } }
package com.coffeebar.coffee; public class LongBlack extends Coffee { public LongBlack() { super.setDescription("LongBlack"); super.setPrice(5.5f); } }
package com.coffeebar.coffee; public class ShortBlack extends Coffee { public ShortBlack() { super.setDescription("ShortBlack"); super.setPrice(6.0f); } }
package com.coffeebar.decorator; import com.coffeebar.main.Drink; public class Decorator extends Drink { private Drink Obj; public Decorator(Drink Obj) { this.Obj = Obj; } @Override public float cost() { // TODO Auto-generated method stub return super.getPrice() + Obj.cost(); } public String Description() { return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription(); } }
package com.coffeebar.decorator; import com.coffeebar.main.Drink; public class Chocolate extends Decorator { public Chocolate(Drink Obj) { super(Obj); // TODO Auto-generated constructor stub super.setDescription("Chocolate"); super.setPrice(3.0f); } }
package com.coffeebar.decorator; import com.coffeebar.main.Drink; public class Milk extends Decorator { public Milk(Drink Obj) { super(Obj); // TODO Auto-generated constructor stub super.setDescription("Milk"); super.setPrice(2.0f); } }
package com.coffeebar.decorator; import com.coffeebar.main.Drink; public class Soy extends Decorator { public Soy(Drink Obj) { super(Obj); // TODO Auto-generated constructor stub super.setDescription("Soy"); super.setPrice(1.5f); } }
package com.coffeebar.main; import com.coffeebar.coffee.Decaf; import com.coffeebar.coffee.LongBlack; import com.coffeebar.decorator.Chocolate; import com.coffeebar.decorator.Milk; public class Coffeebar { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Drink order; order = new Decaf(); System.out.println("order1 price: " + order.cost()); System.out.println("order1 desc: " +order.getDescription()); System.out.println("*******************"); order = new LongBlack(); order = new Milk(order); order = new Chocolate(order); order = new Chocolate(order); System.out.println("order2 price: " + order.cost()); System.out.println("order2 desc: " + order.getDescription()); } }
输出结果
order1 price: 3.0 order1 desc: Decaf ******************* order2 price: 13.5 order2 desc: Chocolate