I think everyone has ever played QQ Show image dress up, which reflects the decoration mode in our big talk design model. I remember that I used to change a cool dress for my QQ. Next we will use the decoration mode to make a cool dress for Zhang Xiao's beauty.
First, define a person class, which is equivalent to concretecoment, a specific decoration object.
Namespace cool MM _ {class person {public person () {} private string name; Public Person (string name) {This. name = Name;} Public Virtual void show () {console. writeline ("Dress up {0}", name );}}
Secondly, define a finery class, which is equivalent to decorator. An abstract decoration class inherits from person. It contains objects that implement the component interface.
Class finery: person {protected person component; // dress up person in public void decorte (person component) {This. component = component;} public override void show () {If (Component! = NULL) {component. Show ();}}
Afterwards, it is a specific decoration subclass, which is equivalent to concretedecorator and mainly used to add responsibilities.
Class curlyhair: finery {public override void show () {console. write ("curly hair"); base. show () ;}} class Sunglasses: finery {public override void show () {console. write ("sunglasses"); base. show () ;}} class leathercoat: finery {public override void show () {console. write ("leather"); base. show () ;}} class boot: finery {public override void show () {console. write ("Boots"); base. show ();}}
Finally, the client displays the Code:
Static void main (string [] ARGs) {person XC = new person ("leng Mei Zhang Xiao"); // instantiate the specific decoration subclass curlyhair pqx = new curlyhair (); sunglasses KK = new sunglasses (); leathercoat dx = new leathercoat (); boot Km = new boot (); KK. decorte (pqx); dx. decorte (kk); km. decorte (dx); pqx. decorte (XC); km. show (); console. read ();}}}
In this way, Zhang Xiao, who has long curly hair, sunglasses, leather clothing, and boots, is shown in front of us!
What is the decoration mode? Why should we use it?
Decoration mode: it refers to dynamically adding some additional responsibilities to an object. In addition, the decoration mode is more flexible than the subclass generation. In general, the decoration mode uses the "instant-use" method to dynamically add system functions.
Application of the decoration mode:
1. You need to extend the functions of a class or append a function to a class.
2. A function needs to be dynamically added to an object, which is different from the Inheritance Mechanism (static ).
3. You need to add a large number of functions generated by the arrangement and combination of some basic functions to make the inheritance relationship less realistic.
Let's take a look at the structure of the decorative chart:
Note:
1. the decorator mode uses a combination rather than an Inheritance Method to dynamically add and Expand functions to the object at runtime. This avoids the disadvantages of poor flexibility of the Inheritance Mechanism, this will not cause a sharp increase in the number of classes.
2. The component class acts as an abstract interface in the decorator mode and does not implement specific behaviors. The decorator class extends the functionality of the component class from the outside, but for the component, you do not need to know the existence of the decorator.
3. concretedecorator is a specific decoration class used to add responsibilities to concretecomponent.
Features of the decoration mode:
1. The decoration object and the real object have the same interface, so that the client object can interact with the real object in the same way as the decoration object.
2. A decoration object contains a reference to a real object.
3. The decoration object accepts all requests from the client. It forwards these requests to real objects.
4. The decoration object can add some additional functions before or after forwarding these requests. This ensures that you can add additional functions externally without modifying the structure of the given object during runtime. In object-oriented systems, inheritance is usually used to extend the functions of a given class.