当前位置:网站首页>『怎么用』装饰者模式
『怎么用』装饰者模式
2022-07-25 09:16:00 【高级摸鱼工程师】
阐述
利用继承设计子类的行为,是在编译时静态决定的,且所有的子类都会继承到这个行为。如果用组合的做法扩展对象的行为,就可以在运行时动态的进行扩展。
装饰者模式:动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
OO原则:对扩展开放,对修改关闭。
原理

- Component是被装饰对象的爸爸,是个接口,定义被装饰者的行为
- Decorator是装饰者的爸爸,它继承了Component,保证了装饰者和被装饰者的类型一致
- Decorator的儿子们都持有一个Component对象的引用,用以对Compoment行为做“装饰”
实现
public interface Component {
String DESCRIPTION = "我是component";
default String getDescription(){
return DESCRIPTION;
}
/** * 装饰着扩展的行为 */
String link();
}
public interface Decorator extends Component {
/** * 装饰者覆盖这个方法添加自己的属性定义,或者说是扩展这个方法 * @return */
@Override
String getDescription();
}
public class ConcreteComponentB implements Component {
@Override
public String link() {
return "我是Component的二儿子对link的实现-->";
}
}
public class ConcreteDecoratorB implements Decorator {
private Component component;
public ConcreteDecoratorB(Component component) {
this.component = component;
}
@Override
public String getDescription() {
return component.getDescription() + "-->我是包装者B的描述";
}
@Override
public String link() {
return "Decorator的二女儿对Component的link做了装饰-->" + component.link();
}
}
测试类
public class TestDrive {
public static void main(String[] args) {
Component componentB = new ConcreteComponentB();
componentB = new ConcreteDecoratorA(componentB);
componentB = new ConcreteDecoratorB(componentB);
System.out.println("componentB.getDescription() = " + componentB.getDescription());
System.out.println("componentB.link() = " + componentB.link());
}
}
输出
componentB.getDescription() = 我是component-->我是包装者A的描述-->我是包装者B的描述
componentB.link() = Decorator的二女儿对Component的link做了装饰-->
Decorator的大女儿对Component的link做了装饰-->我是Component的二儿子对link的实现-->
总结
- 装饰者模式用了一个名词叫“组合”,其实其灵魂是“多态”,看清楚,不是“变态” :)
- 其实用法的思路很简单,搞一个“装饰者”接口继承“被装饰者”并且@Override想要“装饰”的方法【不是所有方法,要不然你这不是装饰是拆迁】,然后不同的装饰者实现类去对这个方法做装修。装修的前提是持有一个被装饰者的引用,只有这样,才能对被装饰者中已经实现的方法“动手动脚”【人都在你屋子里了,你还不是想干嘛就干嘛:)】
Q&A
Q:这么多Component和Decorator的实现类,岂不是生活“类爆炸”?
A:搞搞“工厂模式”嘛
PS : 西八,又是枯燥的一天 (¯﹃¯)
边栏推荐
- [common tools] obtain system status information based on psutil and gputil
- activemq--延迟投递和定时投递
- mysql中的数据结果排名
- [C language] dynamic memory management, flexible array
- [STL]stack&queue模拟实现
- Do you know these methods of MySQL database optimization?
- flink sql怎么持久化?
- JMeter test plan cannot be saved solution
- sql注入
- Common tool classes under JUC package
猜你喜欢

超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张

Troubleshooting error: NPM install emojis list failed

js小游戏源码魔塔闯关下载

分享一个避免递归的部门设计方法

Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL

js触屏小游戏源码冰雪之旅

360度拖拽全景图插件tpanorama.js
[learn rust together] a preliminary understanding of rust package management tool cargo

table表格展开内部行切换效果
![[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line](/img/67/b8c397d78a675014b5e08ceefc88dc.png)
[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line
随机推荐
Common tool classes under JUC package
js弹出式城市筛选组件匹配手机移动端
JDBC的API解析
What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
全网最简约的sklearn环境配置教程(百分百成功)
JS small game source code magic tower breakthrough Download
nacos2.1.0集群搭建
Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?
activemq--可持久化机制之JDBC代码
富文本样式文字图片处理
[buuctf-n1book][Chapter 2 advanced web]ssrf training
Shell script
Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)
API parsing of JDBC
Write two channel (stereo) immediately Wav file
OmniPeek packet capturing tool
OpenCV实现简单的人脸追踪
Cool canvas animation shock wave JS special effect
360度拖拽全景图插件tpanorama.js
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL