-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge_pattern.py
More file actions
75 lines (59 loc) · 1.79 KB
/
bridge_pattern.py
File metadata and controls
75 lines (59 loc) · 1.79 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from abc import ABC, abstractmethod
class Device(ABC):
@abstractmethod
def power_on(self): ...
@abstractmethod
def volume_up(self): ...
@abstractmethod
def volume_down(self): ...
class TV(Device):
def __init__(self):
self.power = 'off'
self.volume = 0
def power_on(self) -> str:
self.power = 'on'
return self.power
def volume_up(self) -> int:
self.volume += 1
return self.volume
def volume_down(self):
self.volume -= 1
return self.volume
class Radio(Device):
def __init__(self):
self.power = 'off'
self.volume = 0
def power_on(self) -> str:
self.power = 'on'
return self.power
def volume_up(self) -> int:
self.volume += 1
return self.volume
def volume_dowm(self):
self.volume -= 1
return self.volume
class Controller(ABC):
@abstractmethod
def power_button(self): ...
@abstractmethod
def volume_up_button(self): ...
@abstractmethod
def volume_down_button(self): ...
class RemoteController(Controller):
def __init__(self, target: Device):
self.target = target
def power_button(self):
self.target.power_on()
print(f"power on, {self.target.__class__.__name__}")
def volume_up_button(self):
self.target.volume_up()
print(f"volume up, {self.target.volume} {self.target.__class__.__name__}")
def volume_down_button(self):
self.target.volume_down()
print(f"volume down, {self.target.volume} {self.target.__class__.__name__}")
if __name__ == '__main__':
tv = TV()
r = RemoteController(tv)
r.power_button()
r.volume_up_button()
r.volume_down_button()