forked from ydf0509/python36patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
结构型模式-适配器模式.py
73 lines (49 loc) · 1.85 KB
/
结构型模式-适配器模式.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
73
# -*- coding: utf-8 -*-
# @Author : ydf
# @Time : 2019/10/8 0008 17:39
"""
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。
这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。
"""
from monkey_print2 import print
class Dog:
def __init__(self, name):
self.name = name
def wangwang(self):
print('my name is' + self.name + '。。。汪汪汪。。。')
def dog_run(self):
print(f'{self.name} is running')
class Cat:
def __init__(self, name):
self.name = name
def miaomiao(self):
print('my name is' + self.name + '。。。喵喵喵。。。')
def cat_run(self):
print(f'{self.name} is running')
class Sheep:
def __init__(self, name):
self.name = name
def miemie(self):
print('my name is' + self.name + '。。。咩咩。。。')
def sheet_run(self):
print(f'{self.name} is running')
class Adapter:
def __init__(self, adapted_methods):
self.__dict__.update(adapted_methods)
def speak(self):
pass
def run(self):
pass
def main():
animals = []
dog = Dog('旺财')
cat = Cat('大脸猫')
sheep = Sheep('喜洋洋')
animals.append(Adapter({'speak': dog.wangwang, 'run': dog.dog_run}))
animals.append(Adapter({'speak': cat.miaomiao, 'run': cat.cat_run}))
animals.append(Adapter({'speak': sheep.miemie, 'run': sheep.sheet_run}))
for a in animals:
a.speak()
a.run()
if __name__ == "__main__":
main()