当前位置:网站首页>『怎么用』装饰者模式
『怎么用』装饰者模式
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 : 西八,又是枯燥的一天 (¯﹃¯)
边栏推荐
- [STL]list模拟实现
- Canvas text JS special effect composed of many circles
- sql注入
- Disable module (attribute node) in LabVIEW
- Comparison between symmetric encryption and asymmetric encryption
- How to write the code of wechat applet implementation tab
- Additional: SQL statement area / county in the lower half (data table)
- [BUUCTF-n1book][第二章 web进阶]SSRF Training
- Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?
- activemq--延迟投递和定时投递
猜你喜欢

Silicon Valley classroom lesson 15 - Tencent cloud deployment
![[BUUCTF-n1book][第二章 web进阶]SSRF Training](/img/29/8894d04b27e0e73c4458c27bd9b935.png)
[BUUCTF-n1book][第二章 web进阶]SSRF Training

API健康状态自检

Leetcode组合总和+剪枝
![[stl]list Simulation Implementation](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[stl]list Simulation Implementation

360度拖拽全景图插件tpanorama.js
![[machine learning] Finally, the important steps of machine learning modeling have been clarified](/img/75/07767ed694502f0c910d35e1447499.jpg)
[machine learning] Finally, the important steps of machine learning modeling have been clarified

超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张
![[STL]stack&queue模拟实现](/img/92/c040c0e937e2666ee179189c60a3f2.png)
[STL]stack&queue模拟实现

Oracle10g单实例数据库升级到哪个版本好,求建议
随机推荐
PL/SQL工具导出sql文件所使用的命令是什么?
JDBC快速入门
table表格展开内部行切换效果
activemq--可持久化机制之JDBC的journal
Kubedm introduction
yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
JS touch screen game source code ice and snow journey
activemq--可持久化机制之JDBC
The hole of scroll view in uniapp
2022-7-14 JMeter pressure test
How to use pixi.js to make simple Parkour games
Redis-哨兵,主从部署详细篇
酷炫canvas动画冲击波js特效
leetcode-238.除自身以外数组的乘积
2022-7-14 JMeter simulates the login of different users for pressure test
PHP date() function does not support processing numbers greater than 2147483648? "Suggested collection"
Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32
『每日一问』LockSupport怎么实现线程等待、唤醒
Write two channel (stereo) immediately Wav file
Oracle10g单实例数据库升级到哪个版本好,求建议