1
+ # Author: Jheison Duban Quiroga Quintero
2
+ # Github: https://github.com/JheisonQuiroga
3
+
4
+ """
5
+ /*
6
+ * EJERCICIO:
7
+ * Explora el "Principio SOLID de Segregación de Interfaces
8
+ * (Interface Segregation Principle, ISP)", y crea un ejemplo
9
+ * simple donde se muestre su funcionamiento de forma correcta e incorrecta.
10
+ *"""
11
+
12
+ """ 4. Principio de Segregación de Interfaces (ISP)
13
+ Los clientes no deben verse obligados a depender de interfaces que no necesitan.
14
+ Evita crear interfaces grandes y complejas que obliguen a las clases a implementar métodos
15
+ que no necesitan. En su lugar utiliza un interfaces más pequeñas y especificas.
16
+ """
17
+
18
+ # Violación del ISP
19
+ from abc import ABC , abstractmethod
20
+
21
+ class Bird (ABC ):
22
+ @abstractmethod
23
+ def fly (self ):
24
+ pass
25
+
26
+ @abstractmethod
27
+ def swim (self ):
28
+ pass
29
+
30
+ class Pinguin (Bird ):
31
+ def fly (self ):
32
+ raise NotImplementedError ("Error, los pinguinos no pueden volar" )
33
+
34
+ def swim (self ):
35
+ print ("El pinguino está nadando" )
36
+
37
+ # Siguiendo el Interface Segregation Principle
38
+
39
+ class BasicBird (ABC ):
40
+ @abstractmethod
41
+ def eat (self ):
42
+ pass
43
+
44
+ class FlyableBird (ABC ):
45
+ @abstractmethod
46
+ def fly (self ):
47
+ pass
48
+
49
+ class SwimmableBird (ABC ):
50
+ @abstractmethod
51
+ def swim (self ):
52
+ pass
53
+
54
+ # Caso de uso
55
+ class Eagle (BasicBird , FlyableBird ):
56
+ def eat (self ):
57
+ print ("El aguila está comiendo" )
58
+
59
+ def fly (self ):
60
+ print ("El aguila está volando" )
61
+
62
+ class Pinguin (BasicBird , SwimmableBird ):
63
+ def eat (self ):
64
+ print ("El pinguino está comiendo" )
65
+
66
+ def swim (self ):
67
+ print ("El pinguino está nadando" )
68
+
69
+
70
+ """
71
+ * DIFICULTAD EXTRA (opcional):
72
+ * Crea un gestor de impresoras.
73
+ * Requisitos:
74
+ * 1. Algunas impresoras sólo imprimen en blanco y negro.
75
+ * 2. Otras sólo a color.
76
+ * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
77
+ * Instrucciones:
78
+ * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
79
+ * 2. Aplica el ISP a la implementación.
80
+ * 3. Desarrolla un código que compruebe que se cumple el principio.
81
+ */
82
+ """
83
+
84
+ # Gestor de impresoras
85
+
86
+ class BWPrint (ABC ):
87
+ @abstractmethod
88
+ def print_bw (self , document ):
89
+ pass
90
+
91
+ class ColorPrint (ABC ):
92
+ @abstractmethod
93
+ def print_to_color (self , document ):
94
+ pass
95
+
96
+ class Scannable (ABC ):
97
+ @abstractmethod
98
+ def scan (self , document ):
99
+ pass
100
+
101
+ class Faxable (ABC ):
102
+ @abstractmethod
103
+ def send_fax (self , document : str , number : str ):
104
+ pass
105
+
106
+ # Implementaciones especificas
107
+ class BasicPrinter (BWPrint ):
108
+ def print_bw (self , document ):
109
+ print (f"Imprimiendo a blanco y negro { document } " )
110
+
111
+
112
+ class ModernPrinter (BWPrint , ColorPrint ):
113
+ def print_bw (self , document ):
114
+ print (f"Imprimiendo a blanco y negro { document } " )
115
+
116
+ def print_to_color (self , document ):
117
+ print (f"Imprimiendo a color { document } " )
118
+
119
+ class MultiFunctionalPrinter (BWPrint , ColorPrint , Scannable , Faxable ):
120
+ def print_bw (self , document ):
121
+ print (f"Imprimiendo a blanco y negro { document } " )
122
+
123
+ def print_to_color (self , document ):
124
+ print (f"Imprimiendo a color { document } " )
125
+
126
+ def scan (self , document ):
127
+ print (f"Escaneando { document } " )
128
+
129
+ def send_fax (self , document , number ):
130
+ print (f"Enviando Fax: { document } al número: { number } " )
131
+
132
+
133
+ # Código de prueba
134
+ def test_printer (printer ):
135
+ if isinstance (printer , BWPrint ):
136
+ printer .print_bw ("Documento de Prueba B/N" )
137
+ if isinstance (printer , ColorPrint ):
138
+ printer .print_to_color ("Documento de Prueba a color" )
139
+ if isinstance (printer , Scannable ):
140
+ printer .scan ("Documento de Prueba" )
141
+ if isinstance (printer , Faxable ):
142
+ printer .send_fax ("Documento de Prueba" , "123456789" )
143
+
144
+
145
+ # Caso de uso
146
+ if __name__ == "__main__" :
147
+ basic_printer = BasicPrinter ()
148
+ modern_printer = ModernPrinter ()
149
+ multi_printer = MultiFunctionalPrinter ()
150
+
151
+ print ("----- Impresora Básica -----" )
152
+ test_printer (basic_printer )
153
+
154
+ print ("\n ----- Impresora Moderna -----" )
155
+ test_printer (modern_printer )
156
+
157
+ print ("\n ----- Impresora Multifuncional -----" )
158
+ test_printer (multi_printer )
0 commit comments