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.

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

相关推荐
geovindu9 天前
go:Decorator Pattern
开发语言·设计模式·golang·装饰器模式
ximu_polaris10 天前
设计模式(c++)-结构型模式-装饰器模式
c++·设计模式·装饰器模式
Q741_14717 天前
设计模式之装饰器模式 理论总结 C++代码实战
c++·设计模式·装饰器模式
Rsun0455118 天前
7、Java 装饰器模式从入门到实战
java·开发语言·装饰器模式
yaaakaaang22 天前
九、装饰器模式
java·装饰器模式
无籽西瓜a1 个月前
【西瓜带你学设计模式 | 第十二期 - 装饰器模式】装饰器模式 —— 动态叠加功能实现、优缺点与适用场景
java·后端·设计模式·软件工程·装饰器模式
砍光二叉树1 个月前
【设计模式】结构型-装饰器模式
设计模式·装饰器模式
短剑重铸之日1 个月前
《ShardingSphere解读》16 改写引擎:如何理解装饰器模式下的 SQL 改写实现机制?
java·数据库·后端·sql·shardingsphere·分库分表·装饰器模式
我爱学习_zwj1 个月前
设计模式-3(装饰器模式)
前端·设计模式·装饰器模式
清水白石0082 个月前
装饰器模式 vs Python 装饰器:同名背后的深度解析与实战融合
数据库·python·装饰器模式