装饰器模式
一、装饰器模式
- 装饰器模式的概念:
装饰器模式 (Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。装饰者可以在所委托被装饰者的行为之前或之后加上自己的行为,以达到特定的目的。
-
模式的结构
![]()
-
抽象组件(Component):需要被装饰的抽象对象
-
具体组件(ConcreteComponent),是被装饰类,他本身是个具有一些功能的完整的类。
-
抽象装饰类(Decorator),实现了Component接口的同时还在内部维护了一个ConcreteComponent的实例,并可以通过构造函数初始化。而Decorator本身,通常采用默认实现,他的存在仅仅是一个声明:我要生产出一些用于装饰的子类了。而其子类才是赋有具体装饰效果的装饰产品类。
-
具体装饰类(ConcreteDecorator):每一种装饰产品都具有特定的装饰效果。可以通过构造器声明装饰哪种类型的ConcreteComponent,从而对其进行装饰。
3.模式的适用场景:
扩展一个类的功能。
动态增加功能,动态撤销。
4.模式的特点:
4.1优点:
装饰类和被装饰类可以独立发展,不会相互耦合
动态的将责任附加到对象身上。在对象功能扩展方面,它比继承更有弹性。
4.2缺点:
多层装饰比较复杂。
二、范例
相信大家都去星巴克喝过咖啡,或者奶茶,我们在买coffe的时候,首先选择一个基本的coffe类型,比如卡布奇洛,然后添加各种佐料:摩卡,豆浆,蒸奶等。基本类型是基础价,佐料又要宁外算钱。
一般的设计方案就是如上图所示那样,但是采用这种设计方案的话,如果有100多种饮料的话,代码的重用率会很低,代码会显得冗长复杂,。

这种设计方案我们把所有的佐料的放到公共父类中,让所有子类都拥有所有的佐料,只是在类中判断到底加没加。但是也有很大的问题,我们把所有的佐料的放到公共父类中,让所有子类都拥有所有的佐料,只是在类中判断到底加没加。当遇到多份问题,和增加调料种类的时候就会显得复杂。在设计模式原则中有一个非常重要的原则,就是开闭原则:对扩展开放,对修改关闭。

-
//创建抽象组件Drink
-
public abstract class Drink {
-
public String description="";
-
private float price=0f;;
-
-
-
public void setDescription(String description)
-
{
-
this.description=description;
-
}
-
-
public String getDescription()
-
{
-
return description+"-"+this.getPrice();
-
}
-
public float getPrice()
-
{
-
return price;
-
}
-
public void setPrice(float price)
-
{
-
this.price=price;
-
}
-
public abstract float cost();
-
-
}
-
public class Coffee extends Drink {
-
-
@Override
-
public float cost() {
-
// TODO Auto-generated method stub
-
return super.getPrice();
-
}
-
}
-
//创建具体组件(ConcreteComponent)Decaf
-
public class Decaf extends Coffee {
-
public Decaf()
-
{
-
super.setDescription("Decaf");
-
super.setPrice(3.0f);
-
}
-
}
-
//创建抽象装饰类(Decorator)
-
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();
-
}
-
-
@Override
-
public String getDescription()
-
{
-
return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();
-
}
-
-
}
-
//创建具体装饰类(ConcreteDecorator)
-
public class Milk extends Decorator {
-
-
public Milk(Drink Obj) {
-
super(Obj);
-
// TODO Auto-generated constructor stub
-
super.setDescription("Milk");
-
super.setPrice(2.0f);
-
}
-
-
}
-
//创建具体装饰类(ConcreteDecorator)
-
public class Chocolate extends Decorator {
-
-
public Chocolate(Drink Obj) {
-
super(Obj);
-
// TODO Auto-generated constructor stub
-
super.setDescription("Chocolate");
-
super.setPrice(3.0f);
-
}
-
-
}
-
//主函数
-
public class CoffeeBar {
-
-
-
public static void main(String[] args) {
-
-
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());
-
-
}
-
}

浙公网安备 33010602011771号