<读书笔记> Thinking in python (Python 设计模式) 3. Proxy and State模式
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
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
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模式相关推荐
- 剑指offer(第二版)读书笔记以及编程题目python版答案(二)
剑指offer(第二版)读书笔记以及编程题目python版答案(二) 题目五:青蛙跳台阶 github地址: https://github.com/ciecus/leetcode_answers/tr ...
- 读书笔记:《流畅的Python》第五章 一等函数
# 一等对象/一等函数 ''' 1.在运行时创建 2.能赋值给变量或数据结构中的元素 3.能作为函数的参数传给函数 4.能作为函数的返回值返回结果 '''# 函数对象本身时function对象的实例d ...
- 读书笔记(十)——python简单爬取企查查网企业信息,并以excel格式存储
2019独角兽企业重金招聘Python工程师标准>>> 今天这个小爬虫是应朋友,帮忙写的一个简单的爬虫,目的是爬取企查查这个网站的企业信息. 编程最终要的就是搭建编程环境,这里我们的 ...
- python读书笔记2000_流畅的Python读书笔记
特殊方法的存在是为了Python解释器调用的,你自己并不需要去调用他们,比如说my_object.len()这种写法是没有的,应该使用len(my_object).在使用len(my_object)的 ...
- 读书笔记(4)——python爬取糗事百科,并存到MySQL中
2019独角兽企业重金招聘Python工程师标准>>> 安装MySQL.使用phpStudy集成工具来安装MySQL服务器,或者可以用USBwebserve进行安装. 打开USBwe ...
- python源码剖析读书笔记总结_《Python源码剖析》读书笔记:内存垃圾回收
Python内存回收的基石是引用计数,"当一个对象的引用被创建或复制时,对象的引用技术加1:当一个对象的引用被销毁时,对象的引用技术减1",如果对象的引用计数减少为0,将对象的所占 ...
- python 爬取企业注册信息_读书笔记(十)——python简单爬取企查查网企业信息,并以excel格式存储...
今天这个小爬虫是应朋友,帮忙写的一个简单的爬虫,目的是爬取企查查这个网站的企业信息. 编程最终要的就是搭建编程环境,这里我们的编程环境是: python3.6 BeautifulSoup模块 lxml ...
- 读书笔记:《流畅的Python》第19章 动态属性和特性
# 第19章 动态属性和特性""" 属性(attribute):数据的属性和处理数据的方法统称属性,方法只是可调用的属性. 特性(property)除此之外,我们海可以创 ...
- 读书笔记:《流畅的Python》第21章 类元编程
# 第21章 类元编程""" 类元编程指的是运行时创建或定制类的技艺1.类是一等对象,任何时候都可以使用函数新建类,而无需使用class关键字2.类装饰器也是函数,不过能 ...
最新文章
- Asp.Net编码模型
- golangsha1解码_golang中几种加密方式的处理
- 租用的服务器CPU使用率高的原因分析与解决办法
- nginx+php-fpm 的配置下,php的错误日志
- Sigma Grid 2.4 探究 1
- (组合数学笔记)Pólya计数理论_Part.5_Pólya定理
- 武汉将投放5亿元消费券,4月19日微信率先开抢
- 【前端规划】来看看我整理的这一份专属技术知识图谱吧~
- Mysql的master,slave的配置
- Spring Boot (30) 上传文件
- 数据结构与算法之-----图(搜索算法)
- 九款优秀的企业项目协作工具推荐
- Windows Server 2008 R2远程桌面服务配置和授权激活
- 12306 终于随随便便撑起洪峰流量了,全面拥抱Redis 6.x!
- 初学爬虫,简单爬取必应壁纸
- 加州大学戴维斯分校 计算机科学,加州大学戴维斯分校计算机科学申请要求详细解读...
- 金融数据获取系列之一(优矿)
- 163企业邮箱申请,163企业邮箱注册方法
- 递归函数c语言结束条件,满足动态条件时退出递归函数
- 奇点大学人工智能专家:人造智能大脑已接近现实