On the application of Agent mode in the design pattern in C + + programming _c language

Source: Internet
Author: User

Lead to agent mode from problems encountered

You can solve the problem in agent mode at least in the following concentrations:

    • Create expensive objects, such as displaying a large picture, we will create the process to the agent to complete, GoF called Virtual proxy (virtual proxies);
    • Create a local local agent for the objects on the network, such as to manipulate an object on a network (especially when the network performance is not good), we put this manipulation process to an agent to complete, GoF called remote proxy;
    • Control access to objects, such as in the Jive forum users of different permissions (such as Administrators, ordinary users, etc.) will be different levels of operation authority, we will give this work to an agent to complete, GoF called the Protection agent (Protection proxy).

UML diagram:

Role:

    • Subject: Abstract role. Declares a common interface between a real object and a proxy object.
    • Proxy: Agent role. The proxy object implements the same interface as the real object, so it can represent the real object at any time. The agent role contains a reference to the real object, so she can manipulate the real object and attach other actions, which is equivalent to encapsulating the real object.
    • Realsubject: Real role. It represents the real object, the object that we end up referencing.

Example:
If you have a factory that starts with a cell phone, but it doesn't want to make it itself now, it's handing over its own stuff to a company that works at Foxconn to produce it, and then it has the following code to build.

Unified Abstract Interface Ifactory

Class Ifactory 
{public 
: 
  ifactory () {} 
  virtual void makeproduct () = 0; 
}; 

Your cell phone factory.

Class Phonefactory:ifactory 
{public 
: 
  phonefactory () {} 
  void Makeproduct () 
  { 
    cout< < "production of mobile Phone" <<endl; 
  } 
; 

The Agent factory specializes in foundry, Fuji Kang

Class Foxconnproxy:ifactory 
{public 
: 
  foxconnproxy (ifactory* Factory) 
  { 
    m_real = factory; 
  } 
  void Makeproduct () 
  { 
    m_real->makeproduct (); 
  } 
Private: 
  ifactory* m_real; 
 

Client:

ifactory* factory = new Phonefactory (); 
foxconnproxy* proxy = new Foxconnproxy (factory); 
Proxy->makeproduct (); 


    look at the UML diagram and the code above you may find that you first access the proxy class and then access the objects that you really want to access. It seems to be a bit of a superfluous taste, but it's not. A proxy class can be preprocessed before a real class executes. than the production of Foxconn mobile phones may insist on the components are qualified, unqualified will not produce. In such as you have a system to achieve the login function, in the user login, the real login class and Proxy login classes have implemented the login interface, different is the proxy class method to increase the user is legitimate judgments, only legitimate to invoke the real login class of the login method. What the user accesses is actually the proxy login method. This is the advantage of the proxy model. And you can change the agent at any time by using the agent mode. There is also a point you will find that real objects and proxies implement the same interface.
   This pattern is somewhat similar to the decorator pattern, but note that they are not the same: one is dynamically adding responsibilities to a class, and one is controlling access to that object. The most important difference is that their structure is different, you know by comparing the UML diagrams of the two models.

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.