Description
The pattern motive is to implement the operation of the real object by introducing a new object (such as small picture and remote proxy object) or to use the new object as an alias of the real object, which is the proxy mode, which is the mode motive of the proxy mode by introducing the proxy object to indirectly access an object.
Common: Analysis Purchasing goods: customer--shopping site---goods, software development: Client--Agent object--real object. The client implements access to the true object through a proxy object.
Proxy mode: Provides a proxy or placeholder for an object, and the proxy object Controls access to the original object.
The definition of proxy mode introduces a new proxy object proxy object that acts as an intermediary between the client object and the target object remove content and services that the customer cannot see or add additional new services that the customer needs
Structure of the proxy mode
The proxy mode consists of the following 3 roles: Subject (abstract theme Role) proxy (proxy theme role) Realsubject (real theme role)
Instance:
#build a server that accepts the following format data, addr represents the address, and content represents the information receivedinfo_struct=dict () info_struct["Addr"]=10000info_struct["content"]=""classserver:content="" defrecv (self,info):Pass defSend (self,info):Pass defShow (self):PassclassInfoserver (Server):defrecv (self,info): Self.content=Inforeturn "recv ok!" defSend (self,info):Pass defShow (self):Print("show:%s"%self.content)"""Infoserver has the function of receiving and sending, and the sending function is reserved because it is temporarily unavailable. A new interface show is added to show what the server is receiving. The data format that you receive must be as shown in Info_struct, where the server accepts only info_struct content fields. So, how to set a white list for this server, so that only the whitelist address can access the server? Modifying the server structure is a method, but this clearly does not conform to the single principle of responsibility in software design principles. On top of that, using proxies is a good way to do this. The proxy configuration is as follows"""classServerproxy:PassclassInfoserverproxy (serverproxy): Server="" def __init__(self,server): Self.server=Serverdefrecv (self,info):returnSelf.server.recv (Info)defShow (self): Self.server.show ()classWhiteinfoserverproxy (infoserverproxy): White_list=[] defrecv (self,info):Try: assertType (info) = =Dictexcept: return "Info Structure is not correct"Addr=info.get ("Addr", 0)if notAddrinchself.white_list:return "Your address isn't in the list." Else: Content=info.get ("content","") returnself.server.recv (content)defAddwhite (SELF,ADDR): self.white_list.append (addr)defRmvwhite (SELF,ADDR): Self.white_list.remove (addr)defClearwhite (self): self.white_list=[]"""The agent has a server field that controls the agent's server object, Infoserverproxy acts as the direct interface proxy for the server, and Whiteinfoserverproxy directly inherits the Infoserverproxy object. Also added the White_list and dialogue list operations. In this way, the server's whitelist access can be achieved by accessing the dialog list agent in the scene. """if __name__=="__main__": Info_struct=dict () info_struct["Addr"] = 10010info_struct["content"] ="Hello world!"Info_server=infoserver () info_server_proxy=whiteinfoserverproxy (info_server)Print(INFO_SERVER_PROXY.RECV (info_struct)) info_server_proxy.show () Info_server_proxy.addwhite (10010) Print(INFO_SERVER_PROXY.RECV (info_struct)) info_server_proxy.show ()
Printing results:
Your address isn't in the list.
SHOW:
Recv ok!
Show:hello world!
Several common proxy modes
Remote proxy: Provides a local proxy object for an object that is located in a different address space, either in the same host or in another host, also known as the Ambassador (Ambassador)
Virtual Proxy: If you need to create an object that consumes a large resource, create an object that consumes relatively little, and the real object is created only when it is needed.
Protection agent (Protect proxy): Control access to an object, can provide different levels of user access to different users
Buffer Proxy: Provides temporary storage space for the results of a target operation so that multiple clients can share the results
Smart Reference Agent (Smart Reference proxy): Provides some extra action when an object is referenced, such as recording the number of times an object is called
Pattern Benefits
Able to coordinate the caller and callee, to some extent, reduce the system's coupling degree the client can program the abstract topic role, add and replace the proxy class without modifying the source code, conform to the open and close principle, the system has good flexibility and scalability
Pattern disadvantage
Because of the addition of proxy objects between the client and the real topic, some types of proxy patterns may cause requests to be processed more slowly (for example, a protection agent) to implement proxy mode with additional work, and some proxy patterns are more complex to implement (such as a remote proxy)
Mode applicable environment
A remote proxy can be used when a client object needs to access an object in the remote host when it is necessary to use an object that consumes less resources to represent an object that consumes more resources, thereby reducing system overhead and running time, you can use a virtual agent when you need to provide a temporary storage space for one of the frequently accessed operations results. For multiple clients to share access to these results you can use a buffer agent when you need to control access to an object, you can use a protection agent when you want to provide different levels of access to different users when you need to provide some additional action for an object's access (reference), you can use the Smart reference agent
Reference Link: https://yq.aliyun.com/articles/70738?spm=a2c4e.11155435.0.0.70711a1cGSkn9j
python-Proxy Mode