4.装饰器模式(Decorator Pattern)

概述

The decorator pattern provides a flexible alternative to subclassing for extending functionality. When using subclassing, different subclasses extend a class in different ways. However, an extension is bound to the class at compile-time and can't be changed at run-time. The decorator pattern allows responsibilities to be added (and removed from) an object dynamically at run-time. It is achieved by defining Decorator objects that

  • implement the interface of the extended (decorated) object (Component) transparently by forwarding all requests to it.
  • perform additional functionality before or after forwarding a request.
    This allows working with different Decorator objects to extend the functionality of an object dynamically at run-time.

针对于功能扩展需求场景,装饰器模式提供一个不用子类化(继承)的灵活的替代方案。当你使用子类化(继承)方案时,子类以不同的方式继承一个类。但是扩展的功能在编译时被绑定到类上并且在运行时不能更改。

相关推荐
崎岖Qiu17 小时前
【设计模式笔记23】:长文解析-深刻理解「装饰器模式」
java·笔记·设计模式·装饰器模式
资生算法程序员_畅想家_剑魔9 天前
Java常见技术分享-10-装饰器模式
java·开发语言·装饰器模式
世洋Blog13 天前
装饰器模式实践:告别臃肿的继承链,优雅解耦初始化状态管理
unity·设计模式·c#·装饰器模式
qq192263813 天前
基于NSGA2的多目标车辆路径规划 目标1为受灾点缺货量最大值最小,目标2为需求点最晚送达时间最小
装饰器模式
syt_101313 天前
设计模式之-装饰器模式
设计模式·装饰器模式
清水白石00813 天前
《Python 装饰器模式与代理模式深度剖析:从语法技巧到架构实战》
python·代理模式·装饰器模式
ZouZou老师23 天前
C++设计模式之装饰器模式:以家具生产为例
c++·设计模式·装饰器模式
__万波__1 个月前
二十三种设计模式(八)--装饰器模式
java·设计模式·装饰器模式
老鼠只爱大米1 个月前
Java设计模式之装饰器模式详解
java·设计模式·装饰器模式·decorator·java设计模式
兵bing1 个月前
设计模式-装饰器模式
设计模式·装饰器模式