装饰器模式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)
相关推荐
砍光二叉树1 天前
【设计模式】结构型-装饰器模式
设计模式·装饰器模式
短剑重铸之日2 天前
《ShardingSphere解读》16 改写引擎:如何理解装饰器模式下的 SQL 改写实现机制?
java·数据库·后端·sql·shardingsphere·分库分表·装饰器模式
我爱学习_zwj5 天前
设计模式-3(装饰器模式)
前端·设计模式·装饰器模式
清水白石0081 个月前
装饰器模式 vs Python 装饰器:同名背后的深度解析与实战融合
数据库·python·装饰器模式
cuber膜拜1 个月前
Tenacity 原理与基本使用
服务器·网络·python·装饰器模式·tenacity
短剑重铸之日2 个月前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式
懵萌长颈鹿2 个月前
装饰器模式 (Decorator Pattern)
装饰器模式
进击的小头2 个月前
创建型模式:装饰器模式(C语言实战指南)
c语言·开发语言·装饰器模式
小码过河.2 个月前
17装饰器模式
开发语言·python·装饰器模式
茶本无香2 个月前
设计模式之七—装饰模式(Decorator Pattern)
java·设计模式·装饰器模式