Decorator mode (Structural Model)
Dynamically attaches responsibility to the object in a transparent manner to the client. This mode provides a more flexible alternative to inheritance to extend the object function. Wrapper is essentially different from wrapper. The adapter needs to change the interface of the object to be considered, but does not necessarily change the performance of the object. The modifier wants to maintain the interface of the object to enhance the object performance.
Combine rather than inherit , Decorator Mode is implemented in Dynamic extension object during runtime Function And can expand multiple functions as needed. This avoids " Poor flexibility " And " Multi-subclass Derivative Problems " .
Decorator Class on the Interface Is-a component , That is Decorator Class inherited Component Class interface. However, the implementation is as follows: Has-a component The combination Decorator Class uses another Component Class.
Decorator Mode is not a solution " Multi-inheritance derived from multiple child classes " Problem, Decorator Solution: " Extension of the Subject Class in multiple directions "-- Yes " Decoration " .
Applicability:
1. dynamically and transparently Add a responsibility to an object without affecting other objects.
2. We hope that the responsibilities and functions can be added or canceled at any time.
3. When the function cannot be extended through class inheritance. (Too many inheritance; class definitions are hidden; class definitions are not easy to generate Derived classes)
Structure:
1. Abstract component (cComponent): defines an object interface and can dynamically attach responsibility to other objects.
2. Specific Component (cconcretecomponent): defines the objects that can be attached with responsibility.
3. cdecorator: maintains a reference to an abstract Part object and defines an interface consistent with the abstract Part interface to "Decorate" the interface of the abstract Part object.
4. The specific modifier (cconcretedecorator): attaches the responsibility to the abstract part to complete the specific "decoration ".
1. Applicability
The decoration mode should be used in the following situations: 1. you need to expand the functions of a class or add additional responsibilities to a class. 2. A function needs to be dynamically added to an object. These functions can be dynamically revoked. 3. A large number of functions are generated by the arrangement and combination of some basic functions to make the inheritance relationship unrealistic.
2. Implementation points
The decoration role inherits the abstract component role, which is also the biggest feature of the decoration mode. The purpose is to add responsibilities to the abstract component and display it as the decorated component;
The purpose of giving a decoration role an instance of the component role is to make the component decorated by multiple decorative objects;
It can be more flexible in specific applications, without the need for abstract components and decorative roles. However, the decoration object inherits the decoration object and has the two characteristics of its instance;
Transparent decoration is generally implemented through expansion before and after the base class method, and translucent decoration is generally implemented through a new interface.
Other browsing
Fantasy RPG (item forging and decorator Mode)
. Net design mode (10): decorator pattern)
Design Pattern in. Net -- decorator pattern(The author's blog)
ReferenceArticle
9. decorator)
Design Mode Study note 15: decorator pattern)