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.

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

相关推荐
清水白石0085 天前
装饰器模式 vs Python 装饰器:同名背后的深度解析与实战融合
数据库·python·装饰器模式
cuber膜拜17 天前
Tenacity 原理与基本使用
服务器·网络·python·装饰器模式·tenacity
短剑重铸之日1 个月前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式
懵萌长颈鹿1 个月前
装饰器模式 (Decorator Pattern)
装饰器模式
进击的小头1 个月前
创建型模式:装饰器模式(C语言实战指南)
c语言·开发语言·装饰器模式
小码过河.1 个月前
17装饰器模式
开发语言·python·装饰器模式
茶本无香1 个月前
设计模式之七—装饰模式(Decorator Pattern)
java·设计模式·装饰器模式
obDLaSfLKr1 个月前
Canoe-Autosar网络管理测试脚本用例CAPL 这适用于Autosar NM主流测试用...
装饰器模式
Geoking.2 个月前
【设计模式】装饰者模式详解
设计模式·装饰器模式
蔺太微2 个月前
装饰器模式(Decorator Pattern)
设计模式·装饰器模式