程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design pattern - structural pattern - proxy pattern

編輯:Python

Catalog

List of articles

  • Catalog
  • The proxy pattern
  • Application scenarios
  • Code example

The proxy pattern

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 .

Application scenarios

  • 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 .

Code example

Entity role :

  • Abstract characters (Subject): Business methods implemented by declaring real roles through interfaces or abstract classes .
  • Real role (Real Subject): Realize abstract roles , Define the business logic to be implemented by the real role , For agent role calls .
  • delegable role (Proxy): Realize abstract roles , Is the agent of the real role , Abstract methods are implemented through business logic methods of real roles , And you can attach your own actions .
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 .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved