装饰器模式
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.