proxy mode

The proxy mode in C++, also known as the Proxy mode, is one of the constructive design modes, which can provide a proxy for other objects to control the access to this object. ,

The so-called proxy refers to a class with the same interface as the proxy element (the proxied object). The client must interact with the proxied target class through the proxy, and the proxy generally completes certain tasks during the interaction (before and after the interaction). special treatment.

The agent mode generally has the following three roles:

1. Abstract role: refers to the public methods provided by the proxy role and the real role, generally an interface.

2. Real role: It is necessary to implement the abstract role interface, which defines the business logic to be implemented by the real role, so that it can be called by the proxy role, that is, the real business logic.

3. Proxy role: It is necessary to implement the abstract role interface, which is the proxy of the real role. The abstract method is realized through the business logic method of the real role, and its own operations can be attached to put the unified process control into the proxy role for processing.
Proxy1.cpp (code before mode change)

class ISubject{
public:
    virtual void process();
};

class RealSubject :public ISubject{
public:
    virtual void process(){
        //...
    }
};

class ClientApp{
    ISubject* subject;
public:
    ClientApp(){
        subject = new RealSubject();
    }

    void DoTask(){
        //...
        subject->process();

        //...
    }
};

Proxy2.cpp (code after proxy mode change)

class ISubject{
public:
    virtual void process();
};

class SubjectProxy : public ISubject{
 public :
     virtual  void process(){
         // A call to RealSunject
         // ... 
    }
};

class ClientApp{
    ISubject* subject;
public:
    ClientApp(){
        subject = new SubjectProxy();
    }

    void DoTask(){
        //...
        subject->process();

        //...
    }
};

 

Reprinted in: https://www.cnblogs.com/zhuifeng-mayi/p/11064389.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135149&siteId=291194637