[design mode] Agent mode proxy

Source: Internet
Author: User

In the book "Design pattern: The basis of reusable object-oriented software" in Gof, the proxy pattern is said: Provide a proxy for other objects to control access to this object. Combining the examples of the above game agents and the following figure, let's analyze it. You used to play a game like this:

Now with the game agent, you are playing the game like this:

What did the proxy server do? It instead of you to interact with the game server. It has a much faster access to game servers than you do with a campus network to access game servers. So, your game delay is down.

The proxy mode is divided into four categories: remote agents, virtual proxies, protection agents, and smart references. These four types are described separately in the following use cases.

UML Class Diagram


Proxy

    1. Save a reference so that the agent can access the entity. If the interface between Realsubject and subject is the same, proxy references subject, which is equivalent to holding a subject pointer in the proxy class, which points to realsubject;
    2. Provides an interface that is the same as the subject interface, so that proxies can be used instead of entities;
    3. Control access to the entity and may be responsible for creating and deleting it;
    4. Other features depend on the type of proxy, for example:
      The remote agent is responsible for encoding the request and its parameters and sending the encoded request to entities in different address spaces;
      Virtual agents can cache additional information about an entity in order to delay access to it;
      The protection agent checks to see if the caller has the necessary access rights to implement a request.

Subject: Define the common interface between Realsubject and proxy, so that proxy can be used wherever the realsubject is used;

Realsubject: Defines the entity that proxy is acting on.

Definition on [DP]: Provides a proxy for other objects to control access to this object. There are four common cases: (1) remote agent, (2) virtual agent, (3) protection agent, (4) Smart Reference. This article mainly introduces two kinds of virtual agent and intelligent reference.

Consider a document editor that can embed graphical objects in a document. Some graphical objects are created with great overhead. But opening the document must be fast, so we should avoid creating all the expensive objects at once when we open the document. In this case, you can use the proxy mode to open the document without opening the drawing object, instead of opening the graphical object's proxy to replace the actual graphic. Until you really need to open the graphic, the agent is still responsible for opening it. This is the example given in the [DP] book. The following is a UML diagram of the proxy mode.

classimage{ Public: Image (stringname): M_imagename (name) {}Virtual~Image () {}Virtual voidShow () {}protected:    stringm_imagename;};classBigimage: Publicimage{ Public: Bigimage (stringname): Image (name) {}~bigimage () {}voidShow () {cout<<"Show Big Image:"<<m_imageName<<Endl;}};classBigimageproxy: Publicimage{Private: Bigimage*M_bigimage; Public: Bigimageproxy (stringName): Image (name), M_bigimage (0) {}    ~bigimageproxy () {DeleteM_bigimage;} voidShow () {if(M_bigimage = =NULL) M_bigimage=Newbigimage (m_imagename); M_bigimage-Show (); }};

int Main () {    new bigimageproxy ("proxy.jpg"// proxy    // The agent is responsible for opening    when required Delete image;     return 0 ;}

Reprint please indicate the source http://blog.csdn.net/wuzhekai1985

In the case of the virtual agent, here is an example of two smart references. One is auto_ptr in C + + and the other is smart_ptr. Realized for a moment. First give the AUTO_PTR code implementation:

template<classT>classauto_ptr { Public:      ExplicitAuto_ptr (T *p =0): Pointee (P) {} auto_ptr (auto_ptr<T>&RHS): Pointee (Rhs.release ()) {}~auto_ptr () {DeletePointee;} Auto_ptr<T>&operator= (auto_ptr<t>&RHS) {          if( This! = &RHS) Reset (Rhs.release ()); return* This; } T&operator*()Const{return*Pointee;} T*operator()Const{returnPointee;} T*Get()Const{returnPointee;} T*release () {T*oldpointee =pointee; Pointee=0; returnoldpointee; }      voidReset (T *p =0)      {          if(Pointee! =p) {Deletepointee; Pointee=p; }          }  Private: T*pointee;  }; 

Reading the above code, we can see that the Auto_ptr class is a proxy, and the client simply needs to manipulate the Auto_prt object without having to deal with the proxy pointer pointee. The benefit of AUTO_PTR is that it provides exceptional security for dynamically allocated objects. Because it uses an object to store resources that need to be freed automatically, it then relies on the destructor of the object to release resources. This way, the customer does not need to focus on the release of the resource, which is done automatically by the Auto_ptr object. One of the keys to implementation is overloading the understanding of reference operators and arrow operators, which makes the use of auto_ptr similar to real pointers.

We know that there is no garbage collection mechanism in C + +, which can be compensated by smart pointers, an implementation of smart pointers is given below, and a policy of reference counting is adopted.

Template <typename t>classsmart_ptr{ Public: Smart_ptr (T*p =0): Pointee (P), COUNT (Newsize_t (1)) { }//the initial count value is 1Smart_ptr (ConstSmart_ptr &rhs): Pointee (Rhs.pointee), COUNT (Rhs.count) {++*count;}//copy constructor, Count plus 1~smart_ptr () {Decr_count ();}//destruction, counting minus 1, minus 0 o'clock for garbage collection, which frees up spacesmart_ptr&operator= (Constsmart_ptr& RHS)//Overloaded assignment operator    {        //assigning a value to itself is also true, because if you assign a value, the counter first minus 1, plus 1, has not changed++*count;        Decr_count (); Pointee=rhs.pointee; Count=Rhs.count; return* This; }      //overloaded arrow operator and dereference operator, no check for pointersToperator() {returnPointee;} ConstToperator()Const{returnPointee;} T&operator*() {return*Pointee;} ConstT &operator*()Const{return*Pointee;} size_t Get_refcount () {return*count; }//Get Reference counter valuePrivate: T*pointee;//actual pointer, being proxiedsize_t *count;//Reference Counter    voidDecr_count ()//counter minus 1    {        if(--*count = =0)         {            Deletepointee; Deletecount; }    }};

[design mode] Agent mode proxy

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.