装饰器模式(Decorator)
目的
在不改变原有对象的基础上,将功能附加到对象上,从而透明且动态的扩展类的功能。
举例
假设你已经做好了一块蛋糕,现在需要将蛋糕打包为生日蛋糕送给客户,你需要:将其装饰为生日蛋糕、打包放入包装盒。
类图
代码
//省略了部分构造方法,注解等东西
public abstract class CakeGoods {
public abstract void makeCake();
}
public abstract class Decorator extends CakeGoods{
public CakeGoods cakeGoods;
public Decorator(CakeGoods cakeGoods) {
this.cakeGoods = cakeGoods;
}
}
public class Cake extends CakeGoods{
public void makeCake() {
System.out.println ("制作蛋糕");
}
}
public class BirthDecorator extends Decorator{
public BirthDecorator(CakeGoods cakeGoods) {
super (cakeGoods);
}
public void makeCake() {
super.cakeGoods.makeCake ();
System.out.println ("加上相应装饰,装饰为生日蛋糕");
}
}
public class BoxDecorator extends Decorator{
public BoxDecorator(CakeGoods cakeGoods) {
super (cakeGoods);
}
public void makeCake() {
super.cakeGoods.makeCake ();
System.out.println ("放入包装盒,打包。");
}
}
public class Main {
public static void main(String[] args) {
CakeGoods cakeGoods=new Cake();
BirthDecorator birth_Cake=new BirthDecorator (cakeGoods);
BoxDecorator box_birth_Cake=new BoxDecorator (birth_Cake);
cakeGoods.makeCake ();
System.out.println ();
birth_Cake.makeCake ();
System.out.println ();
box_birth_Cake.makeCake ();
System.out.println ();
}
}
运行结果
实际应用
比如除了java.io,javax.swing.border,javax.swing. border都使用了装饰器模式。
举例 java.io:
首先,我们可以像下面这样生成一个读取文件的实例。
Reader reader = new FileReader ( "datafile.txt ");
然后,我们也可以像下面这样在读取文件时将文件内容放入缓冲区。
Reader reader - new BufferedReader(
new FileReader ( " datafile.txt");
);
这样,在生成BufferedReader类的实例时,会指定将文件读取到FileReader类的实例中。
优缺点
优点:
接口的透明性
在不改变原有对象情况下动态扩展功能
不同装饰器可以随意排列组合
缺点:
增加了许多类,增加了程序复杂性
动态装饰时多层装饰时会更复杂
参考文档:
转载自:装饰器模式
图解设计模式