C # proxy Mode

Source: Internet
Author: User

Proxy Mode Applications:

Remote proxy provides local representation for an object in different address spaces, which can hide the fact that an object exists in different geological spaces.
Virtual Proxy: Creates objects with high overhead as needed, and stores real objects that take a long time for instantiation through a proxy.
Security Proxy, used to control the access permissions of real objects.
Intelligent Proxy: When a proxy is called, it can process some additional functions.
Case scenario:
Express love to a girl you like. Generally, we have two options: self-serving (self-confident) and using 'matchmaker (SHY ).
In this example, the use of 'matchmaker 'is a proxy action, which is implemented as follows:
Call the main function in proxy mode:Copy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
SchoolGirl jiaojiao = new SchoolGirl ();
Jiaojiao. Name = "Li jiaojiao ";
// Launch the trojan in person
IGiveGift self = new Pursuit (jiaojiao );
Self. GiveChocolate (); // send chocolate
Self. GiveDolls (); // send a doll
Self. GiveFlowers (); // send flowers
// Use the 'matchmaker'
IGiveGift daili = new Proxy (jiaojiao );
Daili. GiveChocolate (); // send chocolate
Daili. GiveDolls (); // send a doll
Daili. GiveFlowers (); // send flowers
Console. ReadKey ();
}
}

The SchoolGirl class represents a girl object and is implemented as follows:Copy codeThe Code is as follows: public class SchoolGirl
{
Private string name;
Public string Name
{
Get;
Set;
}
}

The Pursuit class represents a real thing (the Pursuit of girls). The implementation is as follows:Copy codeThe Code is as follows: public class Pursuit: IGiveGift
{
SchoolGirl mm;
Public Pursuit (SchoolGirl mm)
{
This. mm = mm;
}
Public void GiveDolls ()
{
Console. WriteLine (mm. Name + "show you doll ");
}
Public void GiveFlowers ()
{
Console. WriteLine (mm. Name + "flowers for you ");
}
Public void GiveChocolate ()
{
Console. WriteLine (mm. Name + "send you chocolate ");
}
}

The Proxy class is the representative of the Pursuit class and is implemented as follows:Copy codeThe Code is as follows: public class Proxy: IGiveGift
{
Pursuit gg;
Public Proxy (SchoolGirl mm)
{
This. gg = new Pursuit (mm );
}
Public void GiveDolls ()
{
Gg. GiveDolls ();
}
Public void GiveFlowers ()
{
Gg. GiveFlowers ();
}
Public void GiveChocolate ()
{
Gg. GiveChocolate ();
}
}

Both the Pursuit and Proxy classes inherit the IGiveGift interface and implement the following:Copy codeThe Code is as follows: interface IGiveGift
{
Void GiveDolls ();
Void GiveFlowers ();
Void GiveChocolate ();
}

Let's take a look at the structure of the proxy mode:

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.