7:代理模式


代理模式:为其他对象提供一个代理以控制对这个对象的访问


1
class SchoolGirl: 2 3 def __init__(self,name): 4 self.name = name 5 6 7 class CommonInterface: 8 9 def sendgift(self): 10 pass 11 12 class Pursuit(CommonInterface): 13 14 def __init__(self,mm): 16 self.mm = mm 17 18 def sendgift(self): 19 print('send %s a doll'%self.mm.name) 20 21 22 class Proxy(CommonInterface): 23 24 def __init__(self,mm): #传入SchoolGirl对象mm作为Pursuit的初始化参数传递给其sendgift方法调用 25 self.host = Pursuit(mm) 26 27 def sendgift(self): 28 self.host.sendgift() 29 30 31 jj = SchoolGirl('jiaojiao') 32 33 pp = Proxy(jj) #此地即模仿了代理Proxy才认识jj的情景(实际上是首先接触,直接面对) 34 pp.sendgift() #即是通过调用代理来执行了与主题相同的工作
原文地址:https://www.cnblogs.com/pengsixiong/p/4886588.html