-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_generic.py
54 lines (35 loc) · 1.18 KB
/
adapter_generic.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
class AdapteeInterface:
def specific_request(self, adapter_type): pass
class Adaptee(AdapteeInterface):
def specific_request(self, adapter_type):
print('A very specific request -', adapter_type)
class Target:
def request(self, adapter_type): pass
# Class Adapter
class AdapterClass(Target, Adaptee):
def request(self, adapter_type):
self.specific_request(adapter_type)
# Object Adapter
class AdapterObject(Target):
def __init__(self, adaptee):
self.adaptee = adaptee
def request(self, adapter_type):
self.adaptee.specific_request(adapter_type)
class Client:
__adapter = None
def __init__(self, adapter):
self.__adapter = adapter
def run(self, adapter_type):
self.__adapter.request(adapter_type)
def main_class_adapter_pattern():
adapter_type = 'Class Adapter Patter'
client = Client(AdapterClass())
client.run(adapter_type)
def main_object_adapter_pattern():
adapter_type = 'Object Adapter Patter'
adapter = AdapterObject(Adaptee())
client = Client(adapter)
client.run(adapter_type)
if __name__ == "__main__":
main_class_adapter_pattern()
main_object_adapter_pattern()