Decoration mode: the dynamic topology adds some additional responsibilities to an object. In addition, the decoration mode is more flexible than the subclass generation.
Component is an object interface that can dynamically add responsibilities to these objects.
ConcreteComponent defines a specific object. You can also add some responsibilities to this object.
The Decorator decorative abstract class inherits the Component and extends the Componnt class functions from the external class. However, for Component, you do not need to know the existence of the Decorator.
Component class
ConcreteComponnet class
Decorator class
.component = (component !=
ConcreteDecoratorA
=
ConcreteDecoratprB
Client call instance
Main(= = =
The decoration method is as follows: first, use ConcreteComponent to instantiate cc, then use ConcreteComponentA's instantiated object ca to wrap cc, then use ConcreteComponetB's object cb to package ca, and finally execute cb Operation ()
Setcomponent is actually used to wrap the object. In this way, the implementation of each decoration object is separated from how to use this object. Each decoration object only cares about its own functions and does not need to be added to the object chain.
However, there are also special cases: if there is only one ConcreteComponent class without an abstract Component class, then the Decorator class can be a subclass of ConcreteComponent. Similarly, if there is only one ConcreteDecorator class, there is no need to create a separate Decorator class. Instead, you can combine the responsibilities of Decorator and ConcreteDecorator into one class.
The decoration mode is a way to dynamically add more functions to existing functions.
The advantage is that the decoration function in the class can be removed from the class, which can simplify the original class. Effectively separates the core responsibilities of the class from the decorative functional area.