The proxy pattern , Provide a proxy for other objects , To control the access mode of an object . In some cases , One object is not suitable or cannot directly reference another , The proxy object can mediate between the client and the target object .
long-range (Remote) agent : Provide a local representation for an object located in a different address space . This different address space can be in this machine , Or in another machine . Remote agents are also called ambassadors (Ambassador). The advantage is that the system can hide the details of the network , So that the client does not have to consider the existence of the network .
fictitious (Virtual) agent : Create a resource consuming object as needed , Make this object only be created when needed , for example : Delayed image loading . The advantage of using virtual proxy mode is that the proxy object can load the proxy object when necessary ; The agent can optimize the loading process as necessary . When the loading of a module is very resource consuming , The benefits of virtual agents are obvious .
Protection agency (Protection Proxy ): Control access to the original object . Protection agents should be used differently for objects When you have access to .
Smart reference (Smart Reference) agent : When an object is referenced , Provide some additional operations , For example, record the number of calls to this object .
Entity role :
import abc
# Abstract characters
class Subject(metaclass=abc.ABCMeta):
@abc.abstractmethod
def get_content(self):
pass
@abc.abstractmethod
def set_content(self, content):
pass
# Real role
class RealSubject(Subject):
def __init__(self, filename):
self.filename = filename
f = open(self.filename, "r", encoding="utf-8")
self.content = f.read()
print(" Read the file ...")
f.close()
def get_content(self):
return self.content
def set_content(self, content):
f = open(self.filename, "w", encoding="utf-8")
f.write(content)
f.close()
# Virtual agent
class VirtualProxy(Subject):
def __init__(self, filename):
self.filename = filename
self.subj = None
def get_content(self):
if not self.subj:
self.subj = RealSubject(self.filename)
return self.subj.get_content()
def set_content(self, content):
if not self.subj:
self.subj = RealSubject(self.filename)
return self.subj.set_content(content)
# Protection agency
class ProtectedProxy(Subject):
def __init__(self, filename):
self.filename = filename
self.subj = RealSubject(self.filename)
def get_content(self):
return self.subj.get_content()
def set_content(self, content):
raise PermissionError(" Your permission is insufficient !")
if __name__ == "__main__":
print("--- RealSubject ---")
subj = RealSubject("test.txt") # The file will be read here , And occupy memory .
print(subj.get_content())
#subj.set_content(" Modify the content ...")
print("--- VirtualProxy ---")
subj = VirtualProxy("test.txt") # Virtual proxy only calls get_content File will be read only when , And occupy memory .
#print(subj.get_content())
#subj.set_content(" Modify the content ...")
print("--- ProtectedProxy ---")
subj = ProtectedProxy("test.txt")
#print(subj.get_content())
#subj.set_content(" Modify the content ...") # The protection agent cannot be modified here , Will report a mistake .