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.

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

相关推荐
WKP941815 小时前
装饰器模式和代理模式
代理模式·装饰器模式
ZHE|张恒1 天前
设计模式实战篇(六):装饰器模式 —— 让系统具备“可生长能力”的架构思想
设计模式·装饰器模式
欠你一个bug18 天前
Java设计模式应用--装饰器模式
java·设计模式·装饰器模式
金色熊族18 天前
装饰器模式(c++版)
开发语言·c++·设计模式·装饰器模式
杯莫停丶23 天前
设计模式之:装饰器模式
java·设计模式·装饰器模式
WaWaJie_Ngen25 天前
【设计模式】装饰器模式(Decorator)
设计模式·装饰器模式
YA3331 个月前
java设计模式六、装饰器模式
java·设计模式·装饰器模式
王嘉俊9251 个月前
设计模式--装饰器模式:动态扩展对象功能的优雅设计
java·设计模式·装饰器模式
代码萌新知1 个月前
设计模式学习(五)装饰者模式、桥接模式、外观模式
java·学习·设计模式·桥接模式·装饰器模式·外观模式