首页 > 其他 > 详细

7:代理模式

时间:2015-10-16 23:17:24      阅读:379      评论:0      收藏:0      [点我收藏+]

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


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() #即是通过调用代理来执行了与主题相同的工作

 

7:代理模式

原文:http://www.cnblogs.com/pengsixiong/p/4886588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!