装饰器模式decorator

学习笔记,原文链接

https://refactoringguru.cn/design-patterns/decorator

将对象放入包含行为的特殊封装对象中来为原对象绑定新的行为

调用过程

dart 复制代码
当你调用 encoded.writeData(salaryRecords); 时,控制流首先进入 CompressionDecorator 的 writeData 方法。

在 CompressionDecorator 的 writeData 方法中,调用 super.writeData(compressedData);。这意味着调用的是 DataSourceDecorator 的 writeData 方法。

在 DataSourceDecorator 的 writeData 方法中,执行 wrappee.writeData(data);,这里的 wrappee 是 EncryptionDecorator 的实例。

控制流转到 EncryptionDecorator 的 writeData 方法:

EncryptionDecorator 的 writeData 方法将加密后的数据传递给它的父类 DataSourceDecorator。

最后,在 DataSourceDecorator 中,调用 wrappee.writeData(encryptedData);,这里的 wrappee 是 FileDataSource 的实例,最终将加密后的数据写入文件。
复制代码
总结


![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b066595e4cee480bad7f867318ffedf5.png)
相关推荐
WaWaJie_Ngen1 天前
【设计模式】装饰器模式(Decorator)
设计模式·装饰器模式
YA3333 天前
java设计模式六、装饰器模式
java·设计模式·装饰器模式
王嘉俊92513 天前
设计模式--装饰器模式:动态扩展对象功能的优雅设计
java·设计模式·装饰器模式
代码萌新知19 天前
设计模式学习(五)装饰者模式、桥接模式、外观模式
java·学习·设计模式·桥接模式·装饰器模式·外观模式
笨手笨脚の21 天前
设计模式-装饰器模式
java·设计模式·装饰器模式·结构型设计模式
charlie11451419124 天前
精读C++20设计模式:结构型设计模式:装饰器模式
笔记·学习·设计模式·程序设计·c++20·装饰器模式
奔跑吧邓邓子1 个月前
【C++实战㊹】解锁C++装饰器模式:实战与技巧全解析
c++·实战·装饰器模式
休息一下接着来1 个月前
C++ 装饰器模式
c++·设计模式·装饰器模式
bkspiderx1 个月前
C++设计模式之结构型模式:装饰器模式(Decorator)
c++·设计模式·装饰器模式
new_daimond1 个月前
设计模式-装饰器模式详解
设计模式·装饰器模式