1. Overview
Proxy mode ):Provides a proxy for other objects to control access to this object. The so-called proxy means one person or one institution takes action on behalf of another person or another institution. In some cases, a client does not want or cannot directly reference an object, and the proxy object can play a mediation role between the client and the target object.
The intention of the proxy mode is to provide a proxy for other objects to control access to this object. First, as a proxy, the proxy object must have the same interface as the proxy object, which is very important. In other words, users cannot change because they use or do not use proxies. Second, they need to control the access to objects through proxies. In this case, the proxy objects should be non-transparent to customers who need to be proxies; otherwise, the proxy objects cannot be controlled.
Proxy mode structure:
Note: (1) proxy: Save a reference so that the proxy can access the object. If the realsubject and subject interfaces are the same, the proxy will reference the subject. (2) Subject: Declares the common interface between the real subject and the proxy topic, so that the proxy topic can be used in any place where the real subject is used. (3) realsubject: defines the entity represented by proxy. Basic code:
Public abstract class subject
{
Public abstract void request ();
}
Public class realsubject: Subject
{
Public override void request ()
{
Console. writeline ("Real requests ");
}
}
Public class Proxy: Subject
{
Realsubject;
Public override void request ()
{
If (realsubject = NULL)
{
Realsubject = new realsubject ();
}
Realsubject. Request ();
}
} Call: public class Program
{
Static void main (string [] ARGs)
{
Proxy proxy = new proxy ();
Proxy. Request ();
Console. Read ();
}
}
Copy code
2. Instance
Big talk design model: Chasing mm to make wedding dresses for others
Structure:
Code:
// Present
Public interface givegift
{
Void givedolls ();
Void giveflowers ();
Void givechocolate ();
}
/// <Summary>
/// Proxy
/// </Summary>
Public class Proxy: givegift
{
Pursuit GG;
Public proxy (schoolgirl mm)
{
Gg = new pursuit (mm );
}
Public void givedolls ()
{
GG. givedolls ();
}
Public void giveflowers ()
{
GG. giveflowers ();
}
Public void givechocolate ()
{
GG. givechocolate ();
}
}
/// <Summary>
/// Target
/// </Summary>
Public class pursuit: givegift
{
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 ");
}
}
/// <Summary>
/// Girl
/// </Summary>
Public class schoolgirl
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}
Client:
Static void main (string [] ARGs)
{
Schoolgirl Jiaojiao = new schoolgirl ();
Jiaojiao. Name = "Li Jiaojiao ";
Proxy daili = new proxy (Jiaojiao );
Daili. givedolls ();
Daili. giveflowers ();
Daili. givechocolate ();
Console. Read ();
}
Haha, it seems that you have to fight for girls yourself. Never look for an agent. Otherwise, it is easy to get married for someone else ........
3. Summary
Proxy mode:
1. remote proxy: provides a local representation of an object in different address spaces. In this way, the fact that an object exists in different address spaces can be hidden. In. net
Remote proxy is used for Web Services or remote objects. We are no stranger to Web services. When we add a web reference to a project and reference a WebService, A webreference folder and a file will be generated in the project. In fact, they are proxies, so that the client program can call the proxy to solve the remote access problem.
2. Virtual Proxy: creates an object that consumes a lot of resources as needed, so that this object can be created only when necessary. The advantage of using the virtual proxy mode is that the proxy object can be loaded only when necessary; the proxy can optimize the loading process as necessary. When loading a module is very resource-consuming, the benefits of virtual proxy are very obvious. Using a virtual proxy can sometimes optimize the program performance. For example, when we open an HTML webpage, there are a lot of text and images in it, and you can quickly open it, however, you can see only text, but images can only be seen after being downloaded. Unopened image boxes are replaced by virtual proxies, in this case, the proxy stores the path and size of the real image.
3. Protection Proxy: controls access to an object. If necessary, different users can be granted different levels of permissions. The advantage of proxy protection is that it can check the relevant permissions of the user at run time, and then decide to pass the call to the object to which the proxy is.
4. Smart reference: replaces simple pointers and performs some additional operations when accessing objects.
5. Copy-on-write Proxy: a virtual proxy. The replication (clone) operation is performed only when the client needs it.
6. cache Proxy: provides a temporary storage space for the results of a target operation so that multiple clients can share the results.
7. Firewall Proxy: protects the target and prevents malicious users from approaching it.
8. Synchronization Proxy: enables several users to simultaneously use an object without conflict.
Implementation points:
1. The proxy object and the proxy object follow the same interface.
2. In some cases, you do not need to maintain interface consistency. If encapsulation does need to lose some transparency, it can also be considered as a proxy.
Reprinted from: http://www.cnblogs.com/peida/archive/2008/08/18/1268991.html