1
+ """
2
+ /*
3
+ * EJERCICIO:
4
+ * Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)"
5
+ * y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta.
6
+ */
7
+ """
8
+ 'Sin Aplicar'
9
+ class Caja :
10
+ def __init__ (self ,nombre ) -> None :
11
+ self .estado = False
12
+ self .password = ""
13
+ self .alarma = False
14
+ self .intentos = 0
15
+ self .nombre = nombre
16
+
17
+ def abrir (self ):
18
+ if self .estado != True :
19
+ self .estado = True
20
+ else :
21
+ print (f"{ self .nombre } ya esta abierta" )
22
+ self .mostrar_estado ()
23
+
24
+ def cerrar (self ):
25
+ if self .estado != False :
26
+ self .estado = False
27
+ else :
28
+ print (f"{ self .nombre } ya esta cerrada" )
29
+ return
30
+ self .mostrar_estado ()
31
+
32
+ def cambiar_password (self ,old ,new ):
33
+ if self .verificar (password = "" ):
34
+ if old .lower () == self .password .lower ():
35
+ self .password = new
36
+ else :
37
+ self .intentos += 1
38
+ print (f"La contraseña de { self .nombre } no coincide con la proporcionada: nivel de alerta { self .intentos } /3" )
39
+ if self .intentos >= 3 :
40
+ self .alarma = True
41
+ else :
42
+ print (f"{ self .nombre } esta bloqueado" )
43
+
44
+
45
+ def verificar (self ,password ):
46
+ if self .alarma == False :
47
+ if self .password .lower () == password .lower ():
48
+ return True
49
+ else :
50
+ return False
51
+
52
+ def mostrar_estado (self ):
53
+ if self .estado :
54
+ print (f"{ self .nombre } esta abierta" )
55
+ else :
56
+ print (f"{ self .nombre } esta cerrada" )
57
+
58
+ class CajaCarton (Caja ):
59
+ def __init__ (self , nombre ) -> None :
60
+ super ().__init__ (nombre )
61
+
62
+ def abrir (self ):
63
+ if self .verificar ():
64
+ super ().abrir ()
65
+
66
+ def cerrar (self ):
67
+ super ().cerrar ()
68
+
69
+ def cambiar_password (self , old , new ):
70
+ pass
71
+
72
+ def mostrar_estado (self ):
73
+ super ().mostrar_estado ()
74
+
75
+ def verificar (self ):
76
+ return True
77
+
78
+ class CajaFuerte (Caja ):
79
+ def __init__ (self , nombre ) -> None :
80
+ super ().__init__ (nombre )
81
+
82
+ def abrir (self ,password ):
83
+ if self .verificar (password ):
84
+ super ().abrir ()
85
+ else :
86
+ print (f"{ password } no es la contraseña correcta" )
87
+
88
+ def cerrar (self ):
89
+ super ().cerrar ()
90
+
91
+ def cambiar_password (self , old , new ):
92
+ super ().cambiar_password (old , new )
93
+
94
+ def mostrar_estado (self ):
95
+ super ().mostrar_estado ()
96
+
97
+ def verificar (self ,password ):
98
+ return super ().verificar (password )
99
+
100
+ mi_caja_carton = CajaCarton ("Caja de Carton" )
101
+ mi_caja_fuerte = CajaFuerte ("Caja Fuerte" )
102
+
103
+ mi_caja_carton .abrir ()
104
+ mi_caja_fuerte .abrir ("" )
105
+ print ()
106
+
107
+ mi_caja_carton .cerrar ()
108
+ mi_caja_fuerte .cerrar ()
109
+ print ()
110
+
111
+ mi_caja_carton .cambiar_password ("" ,"1234" )
112
+ mi_caja_fuerte .cambiar_password ("" ,"1234" )
113
+
114
+ print ()
115
+
116
+ mi_caja_carton .abrir ()
117
+ mi_caja_fuerte .abrir ("4321" )
118
+ print ()
119
+
120
+ mi_caja_carton .cerrar ()
121
+ mi_caja_fuerte .cerrar ()
122
+
123
+ 'Aplicado'
124
+ class CajaGenerica :
125
+ def __init__ (self ,nombre ) -> None :
126
+ self .estado = False
127
+ self .nombre = nombre
128
+
129
+ def abrir (self ):
130
+ if self .estado != True :
131
+ self .estado = True
132
+ else :
133
+ print (f"{ self .nombre } ya esta abierta" )
134
+ self .mostrar_estado ()
135
+
136
+ def cerrar (self ):
137
+ if self .estado != False :
138
+ self .estado = False
139
+ else :
140
+ print (f"{ self .nombre } ya esta cerrada" )
141
+ return
142
+ self .mostrar_estado ()
143
+
144
+ def mostrar_estado (self ):
145
+ if self .estado :
146
+ print (f"{ self .nombre } esta abierta" )
147
+ else :
148
+ print (f"{ self .nombre } esta cerrada" )
149
+
150
+ class CajaCarton2 (CajaGenerica ):
151
+ def __init__ (self , nombre ) -> None :
152
+ super ().__init__ (nombre )
153
+
154
+ class CajaFuerte2 (CajaGenerica ):
155
+ def __init__ (self , nombre ) -> None :
156
+ super ().__init__ (nombre )
157
+ self .password = ""
158
+ self .alarma = False
159
+ self .intentos = 0
160
+
161
+ def abrir (self ,password ):
162
+ if self .verificar (password ):
163
+ super ().abrir ()
164
+ else :
165
+ print (f"{ password } no es la contraseña correcta" )
166
+
167
+ def cambiar_password (self ,old ,new ):
168
+ if self .verificar (password = "" ):
169
+ if old .lower () == self .password .lower ():
170
+ self .password = new
171
+ else :
172
+ self .intentos += 1
173
+ print (f"La contraseña de { self .nombre } no coincide con la proporcionada: nivel de alerta { self .intentos } /3" )
174
+ if self .intentos >= 3 :
175
+ self .alarma = True
176
+ else :
177
+ print (f"{ self .nombre } esta bloqueado" )
178
+
179
+ def verificar (self ,password ):
180
+ if self .alarma == False :
181
+ if self .password .lower () == password .lower ():
182
+ return True
183
+ else :
184
+ return False
185
+
186
+ # Prueba
187
+ mi_caja_carton2 = CajaCarton2 ("Caja de Carton 2" )
188
+ mi_caja_fuerte2 = CajaFuerte2 ("Caja Fuerte 2" )
189
+
190
+ mi_caja_carton2 .abrir ()
191
+ mi_caja_fuerte2 .abrir ("" )
192
+
193
+ mi_caja_carton2 .cerrar ()
194
+ mi_caja_fuerte2 .cerrar ()
195
+
196
+ mi_caja_carton2 .abrir ()
197
+ mi_caja_fuerte2 .abrir ("" )
198
+
199
+ """
200
+ * DIFICULTAD EXTRA (opcional):
201
+ * Crea un gestor de impresoras.
202
+ * Requisitos:
203
+ * 1. Algunas impresoras sólo imprimen en blanco y negro.
204
+ * 2. Otras sólo a color.
205
+ * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
206
+ * Instrucciones:
207
+ * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
208
+ * 2. Aplica el ISP a la implementación.
209
+ * 3. Desarrolla un código que compruebe que se cumple el principio.
210
+ """
211
+ class ImpresoraBase :
212
+ def __init__ (self ,nombre ) -> None :
213
+ self .nombre = nombre
214
+ def imprimir_bn (self ,documento ):
215
+ print (f"Imprimiendo desde { self .nombre } en blanco y negro: { documento } \n " )
216
+
217
+ class ImpresoraColor (ImpresoraBase ):
218
+ def imprimir_cl (self ,documento ):
219
+ print (f"Imprimiendo desde { self .nombre } en color: { documento } \n " )
220
+
221
+ class Multifuncion (ImpresoraColor ):
222
+ def escanear (self ,documento ):
223
+ print (f"Escaneando desde { self .nombre } : { documento } \n " )
224
+
225
+ def enviar_fax (self ,documento ):
226
+ print (f"Enviando Fax desde { self .nombre } : { documento } \n " )
227
+
228
+ # Prueba
229
+ def test_impresoras (documento ):
230
+ base = ImpresoraBase ("Base" )
231
+ color = ImpresoraColor ("Color" )
232
+ multi = Multifuncion ("Multi" )
233
+
234
+ base .imprimir_bn (documento )
235
+
236
+ color .imprimir_bn (documento )
237
+ color .imprimir_cl (documento )
238
+
239
+ multi .imprimir_bn (documento )
240
+ multi .imprimir_cl (documento )
241
+ multi .escanear (documento )
242
+ multi .enviar_fax (documento )
243
+
244
+ test_impresoras ("Curriculum Vitae" )
0 commit comments