装饰器模式
The Decorator Design Pattern attaches additional responsibilities to an object dynamically. This pattern provide a flexible alternative to subclassing for extending functionality.
装饰器模式动态的给Object添加额外的职责,这个模式为SubClassing提供灵活的扩展功能。
The Decorator Desing Pattern in C# allows us to dynamically add new functionalities to an existing object without altering or modifying its structure and this design pattern acts as a wrapper to the existing class. That mean Decorator Design Pattern dynamically changes the functionality of an object at runtime without impacting the exsiting functionality of the object. In short,this design pattern adds additional functionalities to the object by wrapping it. A decorator is an object that adds features to another object.
UML Class Diagram
Component: This is an interface or abstract calss, contains members that are going to be implemented by the concrete component classes and decorator classes.
ConcreateComponent: This is going to be a concrete class. This class simply implements the component interface.
Decorator: This is an abstract class, This class implements the component interface or inherite abstract class and contains a reference to a component instance. This class also acts as the base class for all decorators.
ConcreteDecorator: This class adds additioal responsibilities to the original component by overriding the interface/abstract method.
Structure Code In C#

/// <summary> /// This is the Base Component that defines operations that can be altered by decorator. /// </summary> public abstract class Component { public abstract void Operation(); } /// <summary> /// Concrete Components provide default implementation of the operations. /// There might be several variations of these classes. /// </summary> public class ConcreteComponent : Component { public override void Operation() { Console.WriteLine($"ConcreteComponent.Operation()"); } }

/// <summary> /// The base Decorator class is also inherited from the same interface as the other concrete component inherited. /// The Primary responsibility of this base decorator class is to define the wrapping interface for all concrete decorators. /// The default implementation of the wrapping code includes a filed for storing a wrapped component and we need to initialize that filed. /// Here, we are initializing that field using the class constructor. /// </summary> public abstract class Decorator : Component { protected Component? component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { component?.Operation(); } } public class ConcreteDecoratorA : Decorator { public override void Operation() { base.Operation(); Console.WriteLine("ConcreteDecoratorA.Operation()"); } } public class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); AddBehavior(); Console.WriteLine("ConcreteDecoratorB.Operation()"); } void AddBehavior() { Console.WriteLine("Add additional behavior."); } } }
When to use the Decorator Design Pattern in real-time application?
- we want to add new functionalities to existing objects dynamically.
- A class definition may be hidden or otherwise unavailable subclasses.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)