装饰者模式
https://i-blog.csdnimg.cn/direct/f8300523d29648b19deea5b4014fe38f.png代码详解:【计划模式】Java 计划模式之装饰者模式(Decorator)_java 装饰者模式-CSDN博客
// 抽象构件角色
public interface Component {
void operation();
}
// 具体构件角色
public class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("执行具体构件对象的操作");
}
}
// 装饰角色
public class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
if (component != null) {
component.operation();
}
}
}
// 具体装饰角色A
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedFunctionA();
}
public void addedFunctionA() {
System.out.println("为构件对象添加功能A");
}
}
// 具体装饰角色B
public class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedFunctionB();
}
public void addedFunctionB() {
System.out.println("为构件对象添加功能B");
}
}
装者模式的应用:
-mybatis 里的缓存:
https://i-blog.csdnimg.cn/direct/3dac155b813d428d8d68396d51f89ad4.pnghttps://i-blog.csdnimg.cn/direct/fdaa418b276b400fa0c13d93321b976b.pnghttps://i-blog.csdnimg.cn/direct/2fb2d2bb61f248179368c1b80201128a.pnghttps://i-blog.csdnimg.cn/direct/3adc754f530148a68d39392d3c29412d.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]