博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python设计模式(四)--代理模式(中)
阅读量:7254 次
发布时间:2019-06-29

本文共 1177 字,大约阅读时间需要 3 分钟。

hot3.png

 

#!/usr/bin/env python# -*- coding: utf-8 -*-# __author__ = 'https://github.com/faif'import timeclass SalesManager:    """真实实体类对象, 它就没有采用公共接口."""    def work(self):        print("Sales Manager working...")    def talk(self):        print("Sales Manager ready to talk")class Proxy:    """    代理类, 需要加载(SalesManager())真实实体类对象,     利用这个真实实体类所提供的方法为外部人员提供服务    """    def __init__(self):        self.busy = 'No'        self.sales = None    def work(self):        print("Proxy checking for Sales Manager availability")        if self.busy == 'No':            self.sales = SalesManager()            time.sleep(2)            self.sales.talk()        else:            time.sleep(2)            print("Sales Manager is busy")class NoTalkProxy(Proxy):    def __init__(self):        Proxy.__init__(self)    def work(self):        print("Proxy checking for Sales Manager availability")        time.sleep(2)        print("This Sales Manager will not talk to you whether he/she is busy or not")if __name__ == '__main__':    p = Proxy()    p.work()    p.busy = 'Yes'    p.work()    p = NoTalkProxy()    p.work()    p.busy = 'Yes'    p.work()

 

转载于:https://my.oschina.net/zhengtong0898/blog/674230

你可能感兴趣的文章
(五)CXF之添加拦截器
查看>>
MySQL系列(四)
查看>>
adb--monkey 压力测试工
查看>>
C语言文件操作函数大全(超详细)
查看>>
sql语句
查看>>
log4j配置
查看>>
安装程序无法创建新的系统分区
查看>>
SpringMVC返回json的问题
查看>>
[LOJ] 分块九题 1
查看>>
DOM
查看>>
C++的特殊工具与技术
查看>>
性能测试方案和性能测试报告小结
查看>>
Springmvc的原理和业务处理
查看>>
【Android】一步实现防重复点击问题
查看>>
网络爬虫的基本实现步骤
查看>>
ajax
查看>>
POJ 2777 线段树
查看>>
python的十进制与任意进制的转换
查看>>
HTTP协议中GET和POST方法的区别
查看>>
malloc calloc 和 realloc
查看>>