Design Pattern Study Notes - Proxy Pattern

      In the proxy mode, the client does not directly call the target object but through a code object, the client calls the proxy object proxy object to call the target object, which plays the role of isolation between objects. Sometimes we don't want to directly access the target object, and sometimes we can't directly access the target object. At this time, we need a proxy object to access the target object instead of us.

A simple example, VPN, we want to access the external network resources due to various reasons, we can access the external network through a proxy server, and the proxy server can access the external network, we access the external network by accessing the proxy server, we do not need to know the proxy server It is how to access the external network specifically as long as it can meet our requirements.

   The proxy mode usually completes the proxy of the target object by holding a reference to the target object through a proxy object. The class diagram is as follows (taken from the Internet):

                 

The proxy mode is very similar to the decorator mode. The proxy mode pays more attention to the access control of the object, while the decorator mode focuses on the enhancement of the object. The proxy mode directly holds the reference of the target object in the proxy object, and The decorator pattern usually needs to pass in the reference of the target object. The relationship between the proxy object and the target object in the proxy pattern is determined by the compiler, while the decorator pattern and the target object are actually determined at runtime.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327065759&siteId=291194637
Recommended