Inherited by the decoration Mode

Source: Internet
Author: User

I recently learned about the Design Pattern of big talk, and deepened my understanding of OO in the process of learning. I have a clear understanding of the differences between abstract and virtual. Today, through the decoration mode, we will review the inheritance of OO, consolidate the constructor calls of child classes on the parent class, and call the parent class method by child classes.

 

First look at a piece of code:

    class Person    {                 public Person()        {            Console.WriteLine("Person“);          }    }
    class Man : Person    {        public Man()        {            Console.WriteLine("Man");          }    }

 

 

Then we instantiate the sub-class man in the main function.

    Man jack = new Man();

What will happen? Beginners who have learned oo know that the result is

When instantiating man, the parent class person is given priority. This is because man, as a subclass, inherits the methods of the parent class person (of course, they are common, that is, public, if it is a father's passbook, the child cannot take it for fun. That is, when the child class is instantiated, the method and attribute that the parent class can inherit will be copied to itself for use. This is a push-down process. From the bottom of the pyramid, we can find the highest-end, and then "show" from top to bottom.

A simple example is easy to understand, and a complicated one is not easy to understand. See the following:

The decoration mode dynamically adds some additional responsibilities to an object. With the addition of functions, the subclass generated by the design mode is more flexible.

Source code is referenced in "big talk design mode"

Person class:

Class person {string name; Public Person () {} public person (string name) {This. name = Name;} Public Virtual void show () {console. writeline ("Dress up {0}", name );}}

 

Finery class: (inherit Person)

    class Finery : Person    {        protected Person component;        public Finery()        {        }        public void Decorator(Person component)        {            this.component = component;        }        public override void show()        {            if (component != null)            {                component.show();            }        }

Miscellaneous: (inheriting finery)

    class LeatherShoes : Finery    {        public override void show()        {            Console.WriteLine("LeatherShoes");            base.show();        }    }
    class BigTrouser : Finery    {        public override void show()        {            Console.WriteLine("Big Trouser");            base.show();        }    }
    class Tshirt : Finery    {        public override void show()        {            Console.WriteLine("T-shirt");            base.show();        }    }

First, let's analyze that leathershoes, bigtrouser, and Tshirt all inherit from finery. That is to say, when instantiating these three classes, the finery constructor is called first. Since the decorator method in finery is public, it is also "copied" to each subclass for their use. Finery inherits from the person class. When the finery constructor is called, it first calls the person constructor and "copies" the person method.

The final result should be:

Instantiate either leathershoes, bigtrouser, or tshirt.

The person constructor is called (there is no parameter)

The show method of person is copied to finery as a virtual method.

Finery constructor called

Finery overwrites the virtual method show.

The constructor of the subclass leathershoes, bigtrouser, and Tshirt is called.

Subclass leathershoes, bigtrouser and Tshirt override the virtual method show

 

It is a reverse push and display process.

We can see in the show method in the subclass leathershoes, bigtrouser, and tshirt.

Console. writeline ("***");
Base. Show ();

What is the role of such a statement?

Like instantiation, it is also a reverse push process, but this time it is not a constructor, but a show method.

For more information, see:

            Person my = new Person("Sonic");            Console.WriteLine("Decorator one:");            LeatherShoes qx = new LeatherShoes();            BigTrouser kk = new BigTrouser();            Tshirt dtx = new Tshirt();            qx.Decorator(my);            kk.Decorator(qx);            dtx.Decorator(kk);            dtx.show();

The parameter "Sonic" is passed in person instantiation. In this case, the person's parameter constructor public person (string name) {This. Name = Name;} is called ;}

 

Leathershoes QX = new leathershoes ();

Bigtrouser KK = new bigtrouser ();

Tshirt dtx = new Tshirt ();

Examples of the three sub-classes have been discussed in the reverse push and display process.

QX. decorator (my); this is the leathershoes object. QX inherits the finery method of the parent class, and the parameter is the person object my.

Let's take a look at what finery's decorator does:

        protected Person component;        public void Decorator(Person component)        {            this.component = component;        }        public override void show()        {            if (component != null)            {                component.show();            }        }

First, define a person object component. In this case, component should be null. If we do not include QX. decorator (my), and directly call show of finery, then component. show () is not executed, that is, show in person is not executed. Now we will pass in my, Component = My, and establish a connection between the two objects of person. At this time, let finery execute show, then the show of component is my show, that is, the person named "Sonic" will be shown. (A little winding)

Of course, we didn't have it execute show here, but passed the parameter through decorator. Because the current process of decoration is carried out, there is no need to display the decoration.

KK. decorator (QX); bigtrouser
The object KK inherits the decorator and passes the leathershoes object QX
 

Dtx. decorator (kk); likewise
 

Dtx. Show (); the last decoration is complete and needs to be displayed. The reverse push process starts:

 

Dtx is the Tshirt object. dtx. Show executes console. writeline ("T-shirt"), and then base. Show ();

Base. Show () is the last layer of show. Who is the last layer of dtx? Of course, it is its parent class finery. Look at dtx. decorator (kk); assign KK to component in finery. Base. Show () is to let the component in the parent class finery, that is, KK show

In this way, KK. Show () will execute console. writeline ("big trouser ");
And then base. Show ();

Similarly, KK performs decorator on QX, so the component of finery is QX, and the base. Show () is QX. Show ();

And so on, and finally to the object my, there is nothing to push up, my. Show () is the virtual method show in person, it will show
The dress name is "sonic.

 

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.