装饰器模式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)
相关推荐
new_daimond3 天前
设计模式-装饰器模式详解
设计模式·装饰器模式
九术沫3 天前
装饰器模式在Spring中的案例
java·spring·装饰器模式
o0向阳而生0o3 天前
102、23种设计模式之装饰器模式(11/23)
设计模式·装饰器模式
宁静致远20213 天前
【C++设计模式】第五篇:装饰器模式
c++·设计模式·装饰器模式
E___V___E5 天前
设计模式--装饰器模式
python·设计模式·装饰器模式
qq_172805598 天前
Go 装饰器模式学习文档
学习·golang·装饰器模式
念念不忘 必有回响11 天前
js设计模式-装饰器模式
javascript·设计模式·装饰器模式
Meteors.11 天前
23种设计模式——装饰器模式(Decorator Pattern)详解
java·设计模式·装饰器模式
pointers_syc1 个月前
【设计模式】4.装饰器模式
设计模式·装饰器模式