Decoration mode Learning

Source: Internet
Author: User
Today, I read the big talk Design Model Book and found that it is really good. I have been wondering about the decoration model. I have understood it tonight. Now I am taking notes. For details, see the relevant sections of this book,
It is mainly parsed.
 

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace decoration Mode
{
Class Program
{
Static void main (string [] ARGs)
{
Person XC = new person ("coriander ");

Console. writeline ("\ n first dress up :");

Sneakers pqx = new sneakers ();
Bigtrouser KK = new bigtrouser ();
Tshirts dtx = new tshirts ();

Pqx. Decorate (XC );
KK. Decorate (pqx );
Dtx. Decorate (kk );
Dtx. Show ();

Console. writeline ("\ n type 2 dress up :");

Leathershoes PX = new leathershoes ();
Tie LD = new tie ();
Suit xz = new suit ();

Px. Decorate (XC );
LD. Decorate (PX );
Xz. Decorate (LD );
Xz. Show ();

Console. writeline ("\ n third dress up :");
Sneakers pqx2 = new sneakers ();
Leathershoes px2 = new leathershoes ();
Bigtrouser kk2 = new bigtrouser ();
Tie LD2 = new tie ();

Pqx2.decorate (XC );
Px2.decorate (pqx );
Kk2.decorate (px2 );
Ld2.decorate (kk2 );

Ld2.show ();

Console. Read ();
}
}

Class person
{
Public Person ()
{}

Private string name;
Public Person (string name)
{
This. Name = Name;
}

Public Virtual void show ()
{
Console. writeline ("Dress up {0}", name );
}
}

Class finery: person
{
Protected Person component;

// Dress up
Public void decorate (person component)
{
This. Component = component;
}

Public override void show ()
{
If (Component! = NULL)
{
Component. Show ();
}
}
}

Class tshirts: finery
{
Public override void show ()
{
Console. Write ("big t-shirt ");
Base. Show ();
}
}

Class bigtrouser: finery
{
Public override void show ()
{
Console. Write ("Pants ");
Base. Show ();
}
}

Class sneakers: finery
{
Public override void show ()
{
Console. Write ("Broken sneakers ");
Base. Show ();
}
}

Class suit: finery
{
Public override void show ()
{
Console. Write ("Suit ");
Base. Show ();
}
}

Class tie: finery
{
Public override void show ()
{
Console. Write ("Tie ");
Base. Show ();
}
}

Class leathershoes: finery
{
Public override void show ()
{
Console. Write ("Shoes ");
Base. Show ();
}
}
}

In fact, in the process of wearing clothes, you should note that the order can be freely matched. So let's take a look at using person XC = new person ("coriander ");
The object instances of the person are created, and the objects of broken sneakers, slashes, and tshirts are initialized respectively.
Let's look at the execution process.
First, let's look at dtx. Show (). First, output "Big T shirt", and then call base. Show (). Then, we will run to the finery class to call the show () method,
However, this is because componet is not null, because dtx. Decorate (kk) takes effect, and dtx is decorated with KK. In this case, you actually need to run KK. Show (),
Output the "Pants" and call base. Show () Again. Similarly, KK is decorated by pqx, --- run pqx. Show (),
Output broken sneakers. In the same way, the final output is "dressing up dishes"

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.