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.

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

相关推荐
Qdd18235 天前
靠谱的义乌秦塘名邸装修哪个好
装饰器模式
geovindu14 天前
CSharp: Decorator Pattern
开发语言·后端·c#·装饰器模式·结构型模式
折哥的程序人生 · 物流技术专研20 天前
Java 23 种设计模式:从踩坑到精通 | 番外:代理 vs 装饰器 —— Spring AOP 到底用了哪个?
java·代理模式·装饰器模式·java面试·java设计模式·springaop·从踩坑到精通
折哥的程序人生 · 物流技术专研2 个月前
Java 23 种设计模式:从踩坑到精通 | 装饰器模式 —— 比继承更灵活的扩展方式,你用过吗?
java·装饰器模式·java面试·结构型模式·java设计模式·javaio·从踩坑到精通
许彰午2 个月前
38_Java设计模式之装饰器模式
java·设计模式·装饰器模式
我登哥MVP2 个月前
走进 Gang of Four 设计模式:装饰器模式
java·spring boot·设计模式·装饰器模式
basketball6162 个月前
设计模式入门:3. 装饰器模式详解 C++实现
c++·设计模式·装饰器模式
咖啡八杯2 个月前
GoF设计模式——装饰模式
java·算法·设计模式·装饰器模式
老码观察2 个月前
设计模式实战解读(六):装饰器模式——功能增强,不动原代码
java·设计模式·装饰器模式
快乐江湖3 个月前
「层层包装」—— 装饰器模式
开发语言·python·装饰器模式