装饰器模式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)
相关推荐
清水白石0087 天前
装饰器模式 vs Python 装饰器:同名背后的深度解析与实战融合
数据库·python·装饰器模式
cuber膜拜19 天前
Tenacity 原理与基本使用
服务器·网络·python·装饰器模式·tenacity
短剑重铸之日1 个月前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式
懵萌长颈鹿1 个月前
装饰器模式 (Decorator Pattern)
装饰器模式
进击的小头1 个月前
创建型模式:装饰器模式(C语言实战指南)
c语言·开发语言·装饰器模式
小码过河.1 个月前
17装饰器模式
开发语言·python·装饰器模式
茶本无香1 个月前
设计模式之七—装饰模式(Decorator Pattern)
java·设计模式·装饰器模式
obDLaSfLKr1 个月前
Canoe-Autosar网络管理测试脚本用例CAPL 这适用于Autosar NM主流测试用...
装饰器模式
Geoking.2 个月前
【设计模式】装饰者模式详解
设计模式·装饰器模式
蔺太微2 个月前
装饰器模式(Decorator Pattern)
设计模式·装饰器模式