forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
112 lines (68 loc) · 1.88 KB
/
mouredev.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Ejercicio
"""
# Sin DIP
from abc import ABC, abstractmethod
class Switch:
def turn_on(self):
print("Enciende la lámpara")
def turn_off(self):
print("Apaga la lámpara")
class Lamp:
def __init__(self) -> None:
self.switch = Switch()
def operate(self, command):
if command == "on":
self.switch.turn_on()
elif command == "off":
self.switch.turn_off()
lamp = Lamp()
lamp.operate("on")
lamp.operate("off")
# Con DIP
class AbstractSwitch:
def turn_on(self):
pass
def turn_off(self):
pass
class LampSwitch(AbstractSwitch):
def turn_on(self):
print("Enciende la lámpara")
def turn_off(self):
print("Apaga la lámpara")
class Lamp:
def __init__(self, switch: AbstractSwitch) -> None:
self.switch = switch
def operate(self, command):
if command == "on":
self.switch.turn_on()
elif command == "off":
self.switch.turn_off()
lamp = Lamp(LampSwitch())
lamp.operate("on")
lamp.operate("off")
"""
Extra
"""
class Notifier(ABC):
@abstractmethod
def send(self, message: str):
pass
class EmailNotifier(Notifier):
def send(self, message: str):
print(f"Enviando email con texto: {message}")
class PUSHNotifier(Notifier):
def send(self, message: str):
print(f"Enviando PUSH con texto: {message}")
class SMSNotifier(Notifier):
def send(self, message: str):
print(f"Enviando SMS con texto: {message}")
class NotificationService:
def __init__(self, notifier: Notifier) -> None:
self.notifier = notifier
def notify(self, message: str):
self.notifier.send(message)
# service = NotificationService(EmailNotifier())
# service = NotificationService(PUSHNotifier())
service = NotificationService(SMSNotifier())
service.notify("¡Hola, notificador!")