1. 简介
装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
2. 定义
-
- 装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
-
- 装饰对象包含一个真实对象的引用(reference)
-
- 装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
-
- 装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。
3. Demo
1)模拟一个创建手机的接口
public interface PhoneOperate {
String operate();
}
2)实现这个接口
public class BasePhoneOperate implements PhoneOperate {
@Override
public String operate() {
return "P70手机";
}
}
被修饰类,这个实现类是真正做事的实现类
3)修饰类
//给手机加个色
public class ColorPhoneOperate implements PhoneOperate{
private PhoneOperate phoneOperate;
public ColorPhoneOperate(PhoneOperate phoneOperate){
this.phoneOperate = phoneOperate;
}
@Override
public String operate() {
return "土豪金" + phoneOperate.operate();
}
}
//给手机加个壳
public class EggPhoneOperate implements PhoneOperate {
private PhoneOperate phoneOperate;
public EggPhoneOperate(PhoneOperate phoneOperate){
this.phoneOperate = phoneOperate;
}
@Override
public String operate() {
return "铝合金壳" + phoneOperate.operate();
}
}
//给手机装上屏保
public class ScreenPhoneOperate implements PhoneOperate{
private PhoneOperate phoneOperate;
public ScreenPhoneOperate(PhoneOperate phoneOperate){
this.phoneOperate = phoneOperate;
}
@Override
public String operate() {
return "装上屏保" + phoneOperate.operate();
}
}
这里创建三个修饰类,作用就是对内部持有的产品进行一定的修饰。
4)模拟业务类
public class MainRun {
public static void main(String[] args) {
//只有这个是真正干事的
BasePhoneOperate basePhoneOperate = new BasePhoneOperate();
//这几个修饰类只起到点缀作用
ColorPhoneOperate colorPhoneOperate = new ColorPhoneOperate(basePhoneOperate);
EggPhoneOperate eggPhoneOperate = new EggPhoneOperate(colorPhoneOperate);
ScreenPhoneOperate screenPhoneOperate = new ScreenPhoneOperate(eggPhoneOperate);
System.out.println( basePhoneOperate.operate());
System.out.println( colorPhoneOperate.operate());
System.out.println( eggPhoneOperate.operate());
System.out.println( screenPhoneOperate.operate());
}
}
4. 使用场景
- Mybatis 的执行器 Executor 接口,二级缓存通过 CachingExecutor 实现,就是个缓存装饰类,真正执行 Sql 的是其内部持有的 Executor。这块刚开始好久看不懂是什么操作,后来悟了。