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: