Design Mode(9): decorator patterns)
Decorator patterns)
Definition
By dynamically adding some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation.
Overview
In software systems, we sometimes use inheritance to extend the functions of objects. However, due to the static characteristics introduced by inheritance as types, this expansion method lacks flexibility; as the number of child classes increases (the number of extended functions), the combination of various child classes (the combination of extended functions) will lead to the expansion of more child classes. How can we enable "Object Function Extension" to be dynamically implemented as needed? At the same time, avoid the subclass expansion problem caused by "more extended functions? So that any "function scaling changes" will cause the lowest impact? This is what we will talk about Decorator Mode. Dynamically add some additional responsibilities to an object. To add a function, Decorator The mode is more flexible than the subclass generation.
decorator patterns) structure chart >
Defines a specific class, or you can add some responsibilities to this object. Decorator: an abstract decorative class that inherits component's function of extending the component class from the external class. However, for component, you do not need to know the existence of the decorator. As for concretedecorator, It is a specific decoration object and serves to add responsibilities to component.
Abstract Class Component
{
Public Abstract Void Operation ();
}
ClassConcretecomponent: Component
{
Public Override VoidOperation ()
{
Console. writeline ("Operations on specific objects");
}
}
Class Decorator: Component
{
Protected Component component;
Public Void Setcomponent (Component component) // Set component
{
This . Component = Component;
}
Public Override Void Operation ()
{ // Override operation (). The actual execution is the operation () of component ()
If (Compoonent ! = Null )
{
Component. Operation ();
}
}
}
Class Concretedecpratora: decorator
{
Private String Addedstate; // Unique functions of this class to distinguish concretebecoratorb
Public Vorride Void Operation ()
{
// First run the operation () of the original component, and then execute the functions of this class, such as addedstate, which is equivalent to decorating the original component.
Base . Operation ();
Addedstate = " New State " ;
Console. writeline ( " Operations of decoration object " );
}
}
Class Concretedecpratora: decorator
{
Public Override Void Operation ()
{
// First run the operation () of the original component, and then execute the functions of this class, such as addedbehavior (), which is equivalent to decorating the original component.
Base . Operation ();
Addedbehavior ();
Console. writeline ( " Operations on decoration object B " );
}
Private Void Addedbehavior () // Unique functions of this class to distinguish concretebecoratora
{
}
}
Static Void Main ( String [] ARGs)
{
Concretecomponent C = New Concretecomponent ();
Concretedecoratora d1 = New Concretedecoratora ();
Concretedecoratorb D2 = New Concretedecoratorb ();
// The decoration method is as follows: first, use concretecomponent to instantiate Object C, and then use concretedecoratora's instantiated object D1 to Package C,
Use the concretedecoratorb object D2 to package D1, and finally execute the D2 operation ()
D1.setcomponent (C );
D2.setcomponent (D1 );
D2.operation ();
}
Summary
In the decoration mode, each function to be decorated is placed in a separate class and the class is used to wrap the object to be decorated. Therefore, when special behavior is required, the customerCodeYou can use the decoration function to package objects in sequence as needed.