当前位置:网站首页>『怎么用』装饰者模式
『怎么用』装饰者模式
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 : 西八,又是枯燥的一天 (¯﹃¯)
边栏推荐
- Additional: SQL statement area / county in the middle half (data table)
- leetcode-238.除自身以外数组的乘积
- Django4.0 + web + MySQL 5.7 realize simple login operation
- 图解LeetCode——919. 完全二叉树插入器(难度:中等)
- Programmers can't SQL? Ashes Engineer: all waiting to be eliminated! This is a must skill!
- 艺术 NFT 的发展之路
- Additional: in the lower division / county (data sheet)
- nacos2.1.0集群搭建
- [machine learning] Finally, the important steps of machine learning modeling have been clarified
- [stl]list Simulation Implementation
猜你喜欢

activemq--消息重试机制

360度拖拽全景图插件tpanorama.js

Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)

Robot jumping problem

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

Silicon Valley class lesson 11 - official account news and wechat authorization

canvas动态图片头像晃动js特效

这家十年内容产业基建公司,竟是隐形的Web3先行者

table表格展开内部行切换效果
![[stl]list Simulation Implementation](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[stl]list Simulation Implementation
随机推荐
mysql中的数据结果排名
[BUUCTF-n1book][第二章 web进阶]SSRF Training
Nacos搭建配置中心出现client error: invalid param. endpoint is blank
[deep learning] overview | the latest progress of deep learning
Cool canvas animation shock wave JS special effect
神经网络学习(1)前言介绍
Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
[STL]stack&queue模拟实现
Sort out Huawei ap-3010dn_ V2 configuration create WiFi
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
对称式加密与非对称式加密的对比
activemq--持久化机制之LevelDB
How does Youxuan database encrypt data?
uniapp中scroll-view的坑
JDBC的API解析
yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
(self drawn ugly picture) simple understanding tcp/ip three handshakes and four waves
Silicon Valley class lesson 11 - official account news and wechat authorization
【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
The garbage classification data set used in the excellent Yolo target detection training is shared - about 3000 labeled