-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathadapter.py
More file actions
53 lines (33 loc) Β· 1.28 KB
/
Copy pathadapter.py
File metadata and controls
53 lines (33 loc) Β· 1.28 KB
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
"""
Adapter pattern - making the incompatible objects adaptable to each other.
Converts the interface of a class into another one that a client is expecting.
For example, Adapter could help convert XML data format to JSON for further analysis.
+ Allows separating the interface from business logic.
+ Adding new adapters doesnβt break the clientβs code
- Increases the code complexity
Bridge and decorator related to Adapter pattern.
"""
class Target:
def request(self):
return "Target: The default target's behavior."
class Adaptee:
def specific_request(self):
return ".eetpadA eht fo roivaheb laicepS"
class Adapter(Target, Adaptee):
def request(self):
return f"Adapter: (TRANSLATED) {self.specific_request()[::-1]}"
def client_code(target: "Target"):
print(target.request())
def main():
print("Client: I can work just fine with the Target objects:")
target = Target()
client_code(target)
adaptee = Adaptee()
print("Client: The Adaptee class has a weird interface. "
"See, I don't understand it:")
print(f"Adaptee: {adaptee.specific_request()}")
print("Client: But I can work with it via the Adapter:")
adapter = Adapter()
client_code(adapter)
if __name__ == "__main__":
main()