I. Introduction
In the process of software development, some objects sometimes cannot or cannot directly access these objects due to network or other obstacles. If you directly access the objects to bring unnecessary complexity to the system, in this case, an intermediate layer can be added between the client and the target object, so that the proxy object replaces the target object. Then, the client only needs to access the proxy object, the proxy object will help us request the target object and return the result to the client. This solution is to introduce the proxy mode today.
Ii. Proxy Mode
The proxy mode can be divided into the following categories by purpose:
Remote) proxy:Provides a local area for objects in different address spaces. The address space can be in the current computer or another computer. The most typical example is that a client calls a Web service or a WCF Service.
Virtual) proxy:Create an object with high resource consumption as needed, so that the object can be created only when necessary.
Copy-on-Write Proxy:A virtual proxy that delays replication or cloning until the client needs it.
Protect or Access) proxy:Controls the access of an object and provides different levels of permissions for different users.
Firewall) proxy:Protection targets prevent malicious users from approaching.
Smart Reference) proxy:When an object is referenced, additional operations are provided, such as recording the number of calls to this object.
Cache Proxy:Provides a temporary storage space for the results of a target operation, so that multiple clients can perform these results.
Among all the proxy modes above, the common proxy modes are virtual proxy, remote proxy, smart reference proxy, and protection proxy. Next let's take a look at the specific definition of the proxy mode.
2.1 Definition
Proxy mode-provides a proxy for an object and controls the reference to the original object by the proxy object. 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. For exampleThe shortcut on the computer desk is a proxy object, and the shortcut is a proxy of the program it references..
2.2 proxy mode implementation
After reading the description of the proxy mode, the following example explains the proxy mode in life. In real life, if a colleague or friend goes abroad, we often drag this friend to help bring electronic products, cosmetics, and other things. In this scenario, a friend who is going abroad is an agent, and he or she is a proxy of his or her friends, because his friend cannot go abroad to buy things, he can, so his friends asked him to help bring something. The following describes how to implement the proxy mode in this scenario. The specific code is as follows:
// The Client calls class Client {static void Main (string [] args) {// creates a proxy object and sends a request to Person proxy = new Friend (); proxy. buyProduct (); Console. read () ;}// abstract topic role public abstract class Person {public abstract void BuyProduct () ;}// real topic role public class RealBuyPerson: person {public override void BuyProduct () {Console. writeLine ("buy an IPhone and an Apple Computer for me") ;}// proxy role public class Friend: Person {// reference the real topic instance RealBuyPerson realSubject; public override void BuyProduct () {Console. writeLine ("Method for accessing real object through proxy class"); if (realSubject = null) {realSubject = new RealBuyPerson ();} this. preBuyProduct (); // call the real theme method realSubject. buyProduct (); this. postBuyProduct ();} // some operations performed by the proxy role public void PreBuyProduct () {// a friend may not know how to ask this friend to bring things, first, this overseas friend wants to list the items that every friend wants to bring and wait for the Console. writeLine ........... ");} // after buying the item, the proxy role must classify the item bought by each friend according to the public void PostBuyProduct () {Console. writeLine ("I have finally bought it. Now I want to split it up. The camera is Michael; the Iphone is Michael Lee's .......... ");}}
There are corresponding comments in the above Code, and there is not much to explain here.
2.3 class graph structure in proxy Mode
After reading the implementation of the proxy mode, the following example analyzes the class graph structure of the proxy mode. The specific class diagram is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12421HJ4-0.png "style =" margin: 0px auto; padding: 0px; border: 0px; "alt =" 27143323-c933932923ff4e47a43f57e0628fac5 "/>
In the preceding class diagram, the proxy mode involves three roles:
Abstract topic role Person ):Declares the public interface of the real subject and proxy topic, so that the proxy topic can be used wherever the real topic is used.
Proxy topic role Friend ):The proxy topic role contains a reference to a real topic to operate on real theme objects. The proxy topic role is responsible for creating real theme objects as needed; A proxy role usually performs some other operations before or after passing a client call to a real topic, rather than simply passing the call to a real topic object. For example, the PreBuyProduct and PostBuyProduct methods here are other operations performed by the proxy topic role.
Real topic role RealBuyPerson ):Defines the real object represented by the proxy role.
Appendix: in the actual development process, when we add service references to the client, some additional classes will be added to the client program, and the classes generated by the client play the proxy topic role, our client also directly calls these proxy roles to access the operations provided by the remote service. This is a typical example of remote proxy.
Iii. Advantages and Disadvantages of proxy Mode
After comprehensively analyzing the proxy mode, let's take a look at the advantages and disadvantages of this mode:
Advantages:
The proxy mode can isolate called objects and reduce the Coupling Degree of the system to a certain extent;
The proxy object acts as an intermediary between the client and the target object, which can protect the target object. The proxy object can perform an additional operation, such as permission check, before sending a request to the target object.
Disadvantages:
Because a proxy object is added between the client and the real topic, the request processing speed is slow.
The implementation of the proxy class also requires additional work, which increases the complexity of the system.
V. Summary
At this point, the introduction of the proxy mode is complete, and the proxy mode provides the proxy for accessing the target object. Now, the introduction of the structural mode is complete. The structural mode includes the adapter mode, Bridge Mode, modifier mode, combination mode, appearance mode, metadata mode, and proxy mode, the following describes the first mode of the behavior mode: Template Method mode.
This article is from the "LearningHard" blog, please be sure to keep this source http://learninghard.blog.51cto.com/6146675/1315891