Simple and Easy-to-solve decoration Mode

Source: Internet
Author: User

I. Purpose:

I was a little dizzy when I first started to look at the decoration mode. After reading it several times, I typed it several times to get some clues, to put it bluntly, the decoration mode is used to dynamically add functions or responsibilities to an object.

In the process of implementation, we will also use the Lee's replacement principle (the premise is that there are multiple decorators, there is an abstract component class, because a component type object is defined in the decorator, the assignment of a concretecomponent object in the main program may be a specific concretecomponent object, and the concretecomponent object is a subclass of component, let the child class Replace the parent class and complete the function is the principle of the Lee's replacement) II. Principle: component: an abstract class or interface that can dynamically add responsibilities to these objects. Concretecomponent: defines a specific object (decorated by). You can add functions and responsibilities to this object (for example, to decorate a wall, the wall is the object) decorator: the abstract class of decoration, inherited from the component class concretedecorator: a specific decoration class (a specific function or accusation), inherited from the decorator class, used to add decoration to the concretecomponent object of the decorator. 3. Run the following code: analyze the implementation part of the code by taking the example of people wearing clothes. 1. this is a concretecomponent class, which is decorated with the public class person {private string name; Public Person (string name) {This. name = Name;} Public Virtual void show () {console. writeline ("Dress up by {0}", name) ;}} 2. the decorator abstract class inherits the person class, which is used to add decoration for the specific object of person (this class will not be used directly, but the subclass of this abstract class) public abstract class decorator: person {protected person = NULL; Public void setperson (person per ){ This. Person = per;} public override void show () {If (person! = NULL) {person. show () ;}} 3. the specific decorative class concretedecorator inherits the decorator class, which is used to add the decoration public class decoratora: decorator {public override void show () {console. writeline ("high heels"); base. show () ;}} public class decoratorb: decorator {public override void show () {console. writeline ("long skirt"); base. show () ;}} 4. code in the main program: static void main (string [] ARGs) {person persona = new person ("Xiaohong"); decoratora A = New decoratora (); decoratorb B = new decoratorb ();. setperson (persona); B. setperson (a); B. show (); console. readkey ();} can be used flexibly in the modifier mode. In the code example above, the component class can be omitted if only one modifier exists, directly declare a concretecomponent class (in the above example, the person class), and the decorator class directly inherits the concretecomponent class. Similarly, if there is only one function to be decorated, you do not need to create a decorator class to directly create a concretedecorator class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.