1
+ """
2
+ # 30 - Prinicipio SOLID: Inversion de dependencias.
3
+ """
4
+ # Lo modulos de alto nivel no deben depender de los modulos de bajo nivel. Ambos deben depender de abstracciones
5
+ # Las abstracciones no deben depender de los detalles. Los detalles deben depender de las abstracciones.
6
+ # Debemos programar hacia interfaces o clases abstractas en lugar de implementaciones concretas.
7
+
8
+ """
9
+ Beneficios
10
+ """
11
+ # Desacoplamiento: Las clases de alto nivel no dependen de las implementaciones específicas.
12
+ # Flexibilidad: Facilita cambiar implementaciones sin modificar código existente.
13
+ # Facilita pruebas unitarias: Permite usar mocks o stubs para las dependencias.
14
+ # Mejor diseño: Fomenta pensar en términos de interfaces y comportamientos, no implementaciones.
15
+
16
+
17
+ """
18
+ Ejemplo de violacion del principio
19
+ """
20
+ # Interruptor depende directamente de LuzConcreta
21
+ # Si queremos usar otro tipo de luz, tendríamos que modificar la clase Interruptor
22
+ # Es difícil realizar pruebas unitarias
23
+
24
+ class LuzPasillo :
25
+ def encender (self ):
26
+ print ("Luz encendida" )
27
+
28
+ def apagar (self ):
29
+ print ('Luz apagada' )
30
+
31
+ class Interruptor :
32
+ def __init__ (self ):
33
+ self .luz = LuzPasillo ()
34
+
35
+ def operar (self ):
36
+ self .luz .encender ()
37
+
38
+
39
+ """
40
+ Ejemplo aplicando el principio
41
+ """
42
+ from abc import ABC , abstractmethod
43
+
44
+ class DispositivoElectronico (ABC ):
45
+ @abstractmethod
46
+ def encender (self ):
47
+ pass
48
+
49
+ def apagar (self ):
50
+ pass
51
+
52
+
53
+ class Luz (DispositivoElectronico ):
54
+ def encender (self ):
55
+ print ('Luz encendida.' )
56
+
57
+ def apagar (self ):
58
+ print ('Luz apagada' )
59
+
60
+
61
+ class Ventilador (DispositivoElectronico ):
62
+ def encender (self ):
63
+ print ('Ventilador encendido' )
64
+
65
+ def apagar (self ):
66
+ print ('Ventilador apagado' )
67
+
68
+
69
+ class Interruptor :
70
+ def __init__ (self , dispositivo : DispositivoElectronico ):
71
+ self .dispositivo = dispositivo
72
+
73
+ def operar (self , encender = True ):
74
+ if encender :
75
+ self .dispositivo .encender ()
76
+ else :
77
+ self .dispositivo .apagar ()
78
+
79
+ luz = Luz ()
80
+ ventilador = Ventilador ()
81
+
82
+ interruptor_luz = Interruptor (luz )
83
+ interruptor_ventilador = Interruptor (ventilador )
84
+
85
+ interruptor_luz .operar (encender = True )
86
+ interruptor_luz .operar (encender = False )
87
+ interruptor_ventilador .operar (encender = True )
88
+
89
+
90
+ """
91
+ Extra
92
+ """
93
+ from abc import ABC , abstractmethod
94
+
95
+ class Notificar (ABC ):
96
+ @abstractmethod
97
+ def enviar_notificacion (self ):
98
+ pass
99
+
100
+ @abstractmethod
101
+ def cancelar_notificacion (self ):
102
+ pass
103
+
104
+
105
+ class Email (Notificar ):
106
+ def enviar_notificacion (self ):
107
+ print ('Se ha enviado el email.' )
108
+
109
+ def cancelar_notificacion (self ):
110
+ print ('Se ha cancelado el email.' )
111
+
112
+ class Push (Notificar ):
113
+ def enviar_notificacion (self ):
114
+ print ('Se ha enviado el Push.' )
115
+
116
+ def cancelar_notificacion (self ):
117
+ print ('Se ha cancelado el Push.' )
118
+
119
+ class Sms (Notificar ):
120
+ def enviar_notificacion (self ):
121
+ print ('Se ha enviado el SMS.' )
122
+
123
+ def cancelar_notificacion (self ):
124
+ print ('Se ha cancelado el SMS.' )
125
+
126
+
127
+ class SistemaNotificaciones :
128
+ def __init__ (self , notificacion : Notificar ):
129
+ self .notificacion = notificacion
130
+
131
+ def procesar_notificacion (self , enviar = True ):
132
+ if enviar :
133
+ self .notificacion .enviar_notificacion ()
134
+ else :
135
+ self .notificacion .cancelar_notificacion ()
136
+
137
+
138
+ email = Email ()
139
+ push = Push ()
140
+ sms = Sms ()
141
+
142
+ enviar_email = SistemaNotificaciones (email )
143
+ enviar_push = SistemaNotificaciones (push )
144
+ enviar_sms = SistemaNotificaciones (sms )
145
+
146
+ enviar_email .procesar_notificacion (enviar = True )
147
+ enviar_email .procesar_notificacion (enviar = False )
148
+ enviar_push .procesar_notificacion (enviar = True )
149
+ enviar_push .procesar_notificacion (enviar = False )
150
+ enviar_sms .procesar_notificacion (enviar = True )
151
+ enviar_sms .procesar_notificacion (enviar = False )
0 commit comments