1.Proxy代理模式

 1 #: c04:ProxyDemo.py
 2 
 3 # Simple demonstration of the Proxy pattern.
 4 
 5 
 6 
 7 class Implementation:
 8 
 9   def f(self): 
10 
11     print "Implementation.f()"
12 
13   def g(self): 
14 
15     print "Implementation.g()" 
16 
17   def h(self): 
18 
19     print "Implementation.h()" 
20 
21 
22 
23 class Proxy:
24 
25   def __init__(self): 
26 
27     self.__implementation = Implementation() 
28 
29   # Pass method calls to the implementation:
30 
31   def f(self): self.__implementation.f() 
32 
33   def g(self): self.__implementation.g() 
34 
35   def h(self): self.__implementation.h() 
36 
37 
38 
39 p = Proxy()
40 
41 p.f(); p.g(); p.h()
42 
43 #<hr>
44 
45 output = '''
46 
47 Implementation.f()
48 
49 Implementation.g()
50 
51 Implementation.h()
52 
53 '''
54 

是一种利用复合(using a)代替继承(is a)的方法,Implementaion不一定需要和Proxy具有相同的接口名称,但是相同的名称更易理解。
Python中的委派机制(delegation)使得Proxy的实现可以非常的简洁优美。如下,利用__getattr__,使得程序具有完整的通用性(generic)
这是动态语言特有的优势,见Dive Into Python第四章,自省的魅力,
"
Python 中万 物皆对象,自省指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作。
利用这种方法,你可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用事先并不知道名称的函数(getattr)
"
 1 #: c04:ProxyDemo2.py
 2 
 3 # Simple demonstration of the Proxy pattern.
 4 
 5 
 6 
 7 class Implementation2:
 8 
 9   def f(self): 
10 
11     print "Implementation.f()"
12 
13   def g(self): 
14 
15     print "Implementation.g()" 
16 
17   def h(self): 
18 
19     print "Implementation.h()" 
20 
21 
22 
23 class Proxy2:
24 
25   def __init__(self): 
26 
27     self.__implementation = Implementation2() 
28 
29   def __getattr__(self, name):
30 
31     return getattr(self.__implementation, name)
32 
33 
34 
35 p = Proxy2()
36 
37 p.f(); p.g(); p.h();
38 
39 #<hr>
40 
41 output = '''
42 
43 Implementation.f()
44 
45 Implementation.g()
46 
47 Implementation.h()
48 
49 '''
50 
2.State模式
与Proxy模式类似,作者认为区别是可以使用不同的implementaion,动态调整使用的implementation.
 1 #: c04:StateDemo.py
 2 
 3 # Simple demonstration of the State pattern.
 4 
 5 
 6 
 7 class State_d:
 8 
 9   def __init__(self, imp): 
10 
11     self.__implementation = imp 
12 
13   def changeImp(self, newImp):
14 
15     self.__implementation = newImp
16 
17   # Delegate calls to the implementation:
18 
19   def __getattr__(self, name):
20 
21     return getattr(self.__implementation, name)
22 
23 
24 
25 class Implementation1:
26 
27   def f(self): 
28 
29     print "Fiddle de dum, Fiddle de dee," 
30 
31   def g(self): 
32 
33     print "Eric the half a bee." 
34 
35   def h(self): 
36 
37     print "Ho ho ho, tee hee hee," 
38 
39 
40 
41 class Implementation2:
42 
43   def f(self): 
44 
45     print "We're Knights of the Round Table." 
46 
47   def g(self): 
48 
49     print "We dance whene'er we're able." 
50 
51   def h(self): 
52 
53     print "We do routines and chorus scenes" 
54 
55 
56 
57 def run(b):
58 
59   b.f()
60 
61   b.g()
62 
63   b.h()
64 
65   b.g()
66 
67 
68 
69 b = State_d(Implementation1())
70 
71 run(b)
72 
73 b.changeImp(Implementation2())
74 
75 run(b)
76 
77 #<hr>
78 
79 output = '''
80 
81 Fiddle de dum, Fiddle de dee,
82 
83 Eric the half a bee.
84 
85 Ho ho ho, tee hee hee,
86 
87 Eric the half a bee.
88 
89 We're Knights of the Round Table.
90 
91 We dance whene'er we're able.
92 
93 We do routines and chorus scenes
94 
95 We dance whene'er we're able.
96 
97 '''
98 

<读书笔记> Thinking in python (Python 设计模式) 3. Proxy and State模式相关推荐

  1. 剑指offer(第二版)读书笔记以及编程题目python版答案(二)

    剑指offer(第二版)读书笔记以及编程题目python版答案(二) 题目五:青蛙跳台阶 github地址: https://github.com/ciecus/leetcode_answers/tr ...

  2. 读书笔记:《流畅的Python》第五章 一等函数

    # 一等对象/一等函数 ''' 1.在运行时创建 2.能赋值给变量或数据结构中的元素 3.能作为函数的参数传给函数 4.能作为函数的返回值返回结果 '''# 函数对象本身时function对象的实例d ...

  3. 读书笔记(十)——python简单爬取企查查网企业信息,并以excel格式存储

    2019独角兽企业重金招聘Python工程师标准>>> 今天这个小爬虫是应朋友,帮忙写的一个简单的爬虫,目的是爬取企查查这个网站的企业信息. 编程最终要的就是搭建编程环境,这里我们的 ...

  4. python读书笔记2000_流畅的Python读书笔记

    特殊方法的存在是为了Python解释器调用的,你自己并不需要去调用他们,比如说my_object.len()这种写法是没有的,应该使用len(my_object).在使用len(my_object)的 ...

  5. 读书笔记(4)——python爬取糗事百科,并存到MySQL中

    2019独角兽企业重金招聘Python工程师标准>>> 安装MySQL.使用phpStudy集成工具来安装MySQL服务器,或者可以用USBwebserve进行安装. 打开USBwe ...

  6. python源码剖析读书笔记总结_《Python源码剖析》读书笔记:内存垃圾回收

    Python内存回收的基石是引用计数,"当一个对象的引用被创建或复制时,对象的引用技术加1:当一个对象的引用被销毁时,对象的引用技术减1",如果对象的引用计数减少为0,将对象的所占 ...

  7. python 爬取企业注册信息_读书笔记(十)——python简单爬取企查查网企业信息,并以excel格式存储...

    今天这个小爬虫是应朋友,帮忙写的一个简单的爬虫,目的是爬取企查查这个网站的企业信息. 编程最终要的就是搭建编程环境,这里我们的编程环境是: python3.6 BeautifulSoup模块 lxml ...

  8. 读书笔记:《流畅的Python》第19章 动态属性和特性

    # 第19章 动态属性和特性""" 属性(attribute):数据的属性和处理数据的方法统称属性,方法只是可调用的属性. 特性(property)除此之外,我们海可以创 ...

  9. 读书笔记:《流畅的Python》第21章 类元编程

    # 第21章 类元编程""" 类元编程指的是运行时创建或定制类的技艺1.类是一等对象,任何时候都可以使用函数新建类,而无需使用class关键字2.类装饰器也是函数,不过能 ...

最新文章

  1. Asp.Net编码模型
  2. golangsha1解码_golang中几种加密方式的处理
  3. 租用的服务器CPU使用率高的原因分析与解决办法
  4. nginx+php-fpm 的配置下,php的错误日志
  5. Sigma Grid 2.4 探究 1
  6. (组合数学笔记)Pólya计数理论_Part.5_Pólya定理
  7. 武汉将投放5亿元消费券,4月19日微信率先开抢
  8. 【前端规划】来看看我整理的这一份专属技术知识图谱吧~
  9. Mysql的master,slave的配置
  10. Spring Boot (30) 上传文件
  11. 数据结构与算法之-----图(搜索算法)
  12. 九款优秀的企业项目协作工具推荐
  13. Windows Server 2008 R2远程桌面服务配置和授权激活
  14. 12306 终于随随便便撑起洪峰流量了,全面拥抱Redis 6.x!
  15. 初学爬虫,简单爬取必应壁纸
  16. 加州大学戴维斯分校 计算机科学,加州大学戴维斯分校计算机科学申请要求详细解读...
  17. 金融数据获取系列之一(优矿)
  18. 163企业邮箱申请,163企业邮箱注册方法
  19. 递归函数c语言结束条件,满足动态条件时退出递归函数
  20. 奇点大学人工智能专家:人造智能大脑已接近现实

热门文章

  1. 小米高管:已投大量精力研发手机AI芯片,造不造还没定
  2. 60秒ICO募资2.35亿,AI+区块链概念,这个风口上的公司有点怪
  3. Cookie、Session和自定义分页
  4. Java运行时数据区域
  5. Android 连接SQLite
  6. YUV格式文件转RGB格式
  7. android之Intent的七大属性
  8. block chain
  9. 重塑营销场景,用友优普助宁波力劲销售管理精细化
  10. 初学Kotlin——在自定义View里的应用