-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.py
72 lines (51 loc) · 1.75 KB
/
adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
python_pattern.examples.adapter
~~~~~~~~~~~
适配器模式
将一个类的接口转换成客户希望的另外一个接 。 Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
:copyright: (c) 2017 by the yinyi.
:license: BSD, see LICENSE for more details.
"""
class Computer:
def __init__(self, name):
self.name = name
def __str__(self):
return 'the {} computer'.format(self.name)
def execute(self):
""" call by client code """
return 'execute a program'
class Synthesizer:
def __init__(self, name):
self.name = name
def __str__(self):
return 'the {} synthesizer'.format(self.name)
def play(self):
return 'is playing an electroinc song'
class Human:
def __init__(self, name):
self.name = name
def __str__(self):
return 'the {} human'.format(self.name)
def speak(self):
return 'says hello'
class Adapter:
def __init__(self, obj, adapted_methods):
""" 不使用继承,使用__dict__属性实现适配器模式 """
self.obj = obj
self.__dict__.update(adapted_methods)
def __str__(self):
return str(self.obj)
# 适配器使用示例
def main():
objs = [Computer('Asus')]
synth = Synthesizer('moog')
objs.append(Adapter(synth, dict(execute=synth.play)))
human = Human('Wnn')
objs.append(Adapter(human, dict(execute=human.speak)))
for o in objs:
# 用统一的execute适配不同对象的方法,这样在无需修改源对象的情况下就实现了不同对象方法的适配
print('{} {}'.format(str(o), o.execute()))
if __name__ == "__main__":
main()