forked from CatalinStefan/python-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.py
More file actions
59 lines (40 loc) · 1.11 KB
/
Copy pathbridge.py
File metadata and controls
59 lines (40 loc) · 1.11 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
from abc import ABC, abstractmethod
class Device(ABC):
volume = 0
@abstractmethod
def get_name(self) -> str:
pass
class Radio(Device):
def get_name(self) -> str:
return f"Radio {self}"
class TV(Device):
def get_name(self) -> str:
return f"TV {self}"
class Remote(ABC):
@abstractmethod
def volume_up(self):
pass
@abstractmethod
def volume_down(self):
pass
class BasicRemote(Remote):
def __init__(self, device: Device):
self.device = device
def volume_up(self):
self.device.volume += 1
print(f"{self.device.get_name()} volume up: {self.device.volume}")
def volume_down(self):
self.device.volume -= 1
print(f"{self.device.get_name()} volume down: {self.device.volume}")
if __name__ == '__main__':
radio = Radio()
tv = TV()
radio_remote = BasicRemote(radio)
tv_remote = BasicRemote(tv)
radio_remote.volume_up()
radio_remote.volume_up()
radio_remote.volume_down()
tv_remote.volume_up()
tv_remote.volume_down()
tv_remote.volume_up()
tv_remote.volume_up()