We have been using Web Services and occasionally use. Net remoting. However, we did not find the technology they used until we learned the proxy mode.
IntroductionProxy mode is widely used. During development, some objects are deployed on another server in the network, such as web service. If you access objects directly on the client, the complexity of client operations will be increased (Handling network communication problems, etc ). At this time, in the application proxy mode, the proxy object of the remote object is created on the client, so that the proxy object can process complex logic, and the client simply calls this proxy object.
In fact, the use of web service is like this: After we add a reference to the Web service on the client, the Web Service proxy is generated in the producer, and all subsequent operations are performed, it is actually an operation on the proxy object.
Example(Simulate Web Service)
1. UML
The proxy mode involves the following roles:
Abstract topic role (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.
Proxy topic (proxy) role:The proxy topic role contains a reference to a real topic, so that you can operate on real theme objects at any time. The proxy topic role provides the same interface as the real topic role, this allows you to replace real subjects at any time. You can control the applications of real themes and create real theme objects (and delete real theme objects) as needed ); A proxy role usually performs an operation before or after passing a client call to a real topic, rather than simply passing the call to a real topic object.
Realsubject role:Defines the real objects represented by the proxy role.
2. Implementation
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Demo1
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Proxy dal= NewProxy ();//The client directly calls the proxy object
Dal. executeinsert ();
Dal. getstring ("I001");
}
}
Interface Idal // Declare unified interfaces
{
VoidExecuteinsert ();
StringGetstring (StringID );
}
Class Proxy: idal // Proxy object implementation
{
Dal cdal;
Public Proxy ()
{
Cdal= NewDal ();
}
Public Void Executeinsert ()
{
Cdal. executeinsert ();
}
Public String Getstring ( String ID)
{
ReturnCdal. getstring (ID );
}
}
Class Dal: idal // Object implementation
{
Public Void Executeinsert ()
{
Console. writeline ("Insert a value to database");
}
Public String Getstring ( String ID)
{
Console. writeline ("Get string from database"+ID );
Return Null;
}
}
}
Description
1) In the example, the proxy class is only forwarded to a specific class. However, in actual applications, pre-requests and post-request processing are usually added before and after the actual request.
2) the main points of proxy mode implementation are clear: the proxy class and the proxy class must have a unified interface, so that uniform implementation can be achieved, ensuring the proxy function implementation.
3) If you want to reduce the complexity of object usage, improve the friendliness of object usage, or improve the efficiency of object usage, you can consider the proxy mode.
Summary
Adding an intermediate layer in a software system is a common method for solving problems.ProxyThe Mode gives us a good implementation.The proxy mode introduces a certain degree of indirect property when accessing objects. Because of this indirect property, we can add more usage and control.
Learning reference:
Http://www.cnblogs.com/Terrylee/archive/2006/05/18/403382.html
Http://www.cnblogs.com/zhenyulu/articles/48128.aspx
Http://www.cnblogs.com/lovecherry/archive/2007/10/08/917363.html
Big talk design mode: proxy Mode