一、代理模式
代理模式,为其他对象提供一种代理,以此控制一个对象的访问方式。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。
二、应用场景
远程(Remote)代理:为一个位于不同的地址空间的对象提供一个局域代表对象。这个不同的地址空间可以是在本机器中,也可是在另一台机器中。远程代理又叫做大使(Ambassador
)。好处是系统可以将网络的细节隐藏起来,使得客户端不必考虑网络的存在。
虚拟(Virtual)代理:根据需要创建一个资源消耗较大的对象,使得此对象只在需要时才会被真正创建,例如:图片延迟加载。使用虚拟代理模式的好处就是代理对象可以在必要的时候才将被代理的对象加载;代理可以对加载的过程加以必要的优化。当一个模块的加载十分耗费资源的情况下,虚拟代理的好处就非常明显。
保护代理(Protection Proxy ):控制对原始对象的访问。保护代理用于对象应该有不同 的访问权限的时候。
智能引用(Smart Reference)代理:当一个对象被引用时,提供一些额外的操作,比如将对此对象调用的次数记录下来等。
三、代码示例
实体角色:
- 抽象角色(Subject):通过接口或抽象类声明真实角色实现的业务方法。
- 真实角色(Real Subject):实现了抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用。
- 代理角色(Proxy):实现了抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import abc # 抽象角色 class Subject(metaclass = abc.ABCMeta): @abc.abstractmethod def get_content( self ): pass @abc.abstractmethod def set_content( self , content): pass # 真实角色 class RealSubject(Subject): def __init__( self , filename): self .filename = filename f = open ( self .filename, "r" , encoding = "utf-8" ) self .content = f.read() print ( "读取文件..." ) 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() # 虚代理 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) # 保护代理 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( "您的权限不够!" ) if __name__ = = "__main__" : print ( "--- RealSubject ---" ) subj = RealSubject( "test.txt" ) # 在这里就会读取文件,并占用内存。 print (subj.get_content()) #subj.set_content("修改内容...") print ( "--- VirtualProxy ---" ) subj = VirtualProxy( "test.txt" ) # 虚代理只有在调用 get_content 时才会读取文件,并占用内存。 #print(subj.get_content()) #subj.set_content("修改内容...") print ( "--- ProtectedProxy ---" ) subj = ProtectedProxy( "test.txt" ) #print(subj.get_content()) #subj.set_content("修改内容...") # 保护代理这里不能进行修改,会报错。 |
到此这篇关于Python设计模式结构型代理模式的文章就介绍到这了,更多相关Python代理模式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://is-cloud.blog.csdn.net/article/details/122930242