Agent mode (Proxy Pattern)

Agency model - to provide an agent to control access to the object to other objects. In some cases, an object is not suitable or not directly refer to another object, the proxy object can play the role of intermediary between the client and the target audience.

Problem scenario: In direct access to the object will cause problems, some objects for some reason (large object creation overhead, or some operations require security controls, or need to access outside the process), will give users direct access or system architecture a lot of trouble, we can add a layer to this access object (for example, only have access to certain interfaces, other interfaces inaccessible, thus can abstract interface) when accessing this object, in order to achieve a complete decoupling of logic and implementation.

advantage:

  • Responsibilities clear. Real role is to implement the actual business logic, do not care about other non-transaction of this responsibility by the latter agency to complete a transaction is completed, the result is programming that comes with simple and clear.
  • Proxy object can play the role of intermediary between the client and the target audience, and the role this played a role in protecting the intermediary of the target object.
  • (The purpose of design patterns, largely because of high scalability) high scalability

Proxy mode consists of:

  • Abstract role: the role of real business methods implemented through an interface or abstract class declaration.
  • Acting roles: to achieve abstract role, the role of agents is true (access layer) to implement the abstract method by real role of business logic methods, and can attach their own operations.
  • Real Role: Implement the abstract roles, define business logic real role to be achieved, calls for acting role.

Schema structure:
a real object you want to access (target class), a proxy object is the real object and the proxy object implements the same interface to access the proxy class and then access the object really want to access.

The concrete realization, in fact, to define an abstract class, the better the agents need to interface definition and then implement a proxy class that implements the abstract class defines the interface, and the work is still referred to the real role complete, the agent role is to achieve a layer of packaging. Acting is lightweight, only to realize the proxy function.

#include<iostream>
using namespace std;

//抽象类
class Subject
{
public:
    Subject(){}
    virtual ~Subject(){}
    //抽象角色:通过接口或抽象类声明真实角色实现的业务方法。
    virtual void Request()=0;//接口
};

//委托类
class ConcreteSubject:public Subject
{
public:
    ConcreteSubject(){}
    ~ ConcreteSubject(){}
    //真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用。
    void Request()
    {
        cout<<"代理实现请求!"<<endl;
    }
};

//代理类
class Proxy
{
public:
    Proxy():_sub(NULL){}
    Proxy(Subject* sub):_sub(sub){}
    ~Proxy(){
        if(_sub!=NULL){
            delete _sub;
        }
    }
    //代理角色:实现抽象角色,是真实角色的代理,
    void Request()
    {
        _sub->Request();    //通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。
    }

private:
    Subject* _sub;
};

int main()
{
    //代理模式的最大好处就是实现了逻辑和实现的彻底解耦
    Subject* sub = new ConcreteSubject();
    Proxy* p = new Proxy(sub);
    p->Request();//p的Request请求实际上是交给了sub来实际执行
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/s-lisheng/p/11331818.html