15. Proxy mode of "interface isolation mode"

1. Motivation

  • In an object-oriented system, for some reasons (for example, the overhead of object creation is very high, or some operations require security control, or require out-of-process access, etc.), direct access will bring problems to the user or the system structure. A lot of trouble.
  • How can the inherent complexity of these objects be managed/controlled without losing the transparency of operating on the objects? Adding a layer of indirection is a common solution in software development.

2. Schema definition

        Provides a proxy for other objects to control (isolate, use interfaces) access to this object.

                                                                                                                ----《Design Patterns》GOF

3. Structure

Client needs to use RealSubject. Under normal circumstances, Client needs to declare a Subject pointer, and then new RealSubject, but due to certain scene restrictions, such as the security considerations mentioned above, or access control, you cannot directly go to new For this specific object, at this time, Proxy can be used to let Cline generate a Proxy object, perform some additional operations in the Proxy, and then mobilize the RealSubject interface. 

4. Code example

Original copy:

        

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();
        // ...
    }
};

But for some reason, the subject can not directly follow the way of new RealSubject

Code Ver2:

class SubjectProxy : public ISubject{
    ISubject *subject;
public:
    virtual void process(){
     //...
     // 对RealSubject的一种间接访问
     subject->process();
     // ...   
    }
};

class ClientApp{
    ISubject *subject;
public:
    ClientApp()
    {
        subject = new SubjectProxy();
    }
    void DoTask(){
        // ...
        subject->process();
        // ...
    }
};

In the Ver2 sample code above, the proxy mode maintains a RealSubject pointer, but in general, it may not be used in this way, but in a more complicated way, but this way of use is also possible.

4. Summary

  • "Adding a layer of indirection" is a common solution to many complex problems in software systems. In an object-oriented system, using some objects directly will cause many problems, and the proxy object as an indirect layer is a common means to solve this problem
  • The implementation methods and granularity of the specific proxy mode are very different (that is, the process method of SubjectProxy in the above ver2 code), some may have fine-grained control over an object, such as copy on write technology, and some may provide abstraction for component modules Proxy layer, proxying objects at the architecture level

        (Some may provide an abstract proxy layer for component modules, and explain the proxying of objects at the architecture level: such as distributed systems, a large number of proxy modes will be used. For example, you want to access a webService. This webService is, for example, Amazon's webService. Access it, including the very popular rust architecture now, you get rust interfaces, such as some rust interfaces of Sina, Weibo, amazon, WeChat, and you access them in C++, there are often certain tools that will help you You generate a proxy class for that interface on the client side. The proxy class is often not written by hand, but is often implemented automatically by tools. Manual writing is very complicated because you have to control all network access details)

  • Proxy does not necessarily require to maintain the complete consistency of the interface, as long as indirect control can be achieved, sometimes it is acceptable to damage some transparency

Guess you like

Origin blog.csdn.net/bocai1215/article/details/127642330