1
+ ### Principio de Segregación de Interfaces (Interface Segregation Principle, ISP)
2
+
3
+ '''
4
+ Este principio establece que:
5
+
6
+ "Ningún cliente debería depender de métodos que no utiliza."
7
+
8
+ Esto significa que las interfaces (o clases abstractas en Python) deben diseñarse de manera que los clientes que
9
+ las implementan no se vean forzados a definir métodos innecesarios o irrelevantes.
10
+
11
+ Ventajas:
12
+
13
+ + Reduce el acoplamiento
14
+ + Hace el código más modular
15
+ + Facilitan la implementación
16
+ '''
17
+
18
+ # Ejemplo que viola el principio
19
+
20
+ from abc import ABC , abstractmethod
21
+
22
+ class Vehicle (ABC ):
23
+
24
+ @abstractmethod
25
+ def drive (self ):
26
+ pass
27
+
28
+ @abstractmethod
29
+ def pilot (self ):
30
+ pass
31
+
32
+ @abstractmethod
33
+ def navigate (self ):
34
+ pass
35
+
36
+ class Car (Vehicle ):
37
+
38
+ def drive ():
39
+ print ("El coche está en marcha" )
40
+
41
+ def pilot (self ):
42
+ raise NotImplementedError ("Un coche no puede volar" )
43
+
44
+ def navigate (self ):
45
+ raise NotImplementedError ("Un coche no puede navegar por el mar" )
46
+
47
+ '''
48
+ En este ejemplo los principales problemas son:
49
+ - El cliente (Car) debe implementar métodos que no utiliza (pilot y navigate)
50
+ - El cliente debe manejar excepciones innecesarias
51
+ '''
52
+
53
+ # Fin Ejemplo que viola el principio
54
+
55
+ # Rediseño para no violar el ISP
56
+
57
+ class Driveable (ABC ):
58
+
59
+ @abstractmethod
60
+ def drive (self ):
61
+ pass
62
+
63
+ class Pilotable (ABC ):
64
+
65
+ @abstractmethod
66
+ def pilot (self ):
67
+ pass
68
+
69
+ class Navigateable (ABC ):
70
+
71
+ @abstractmethod
72
+ def navigate (self ):
73
+ pass
74
+
75
+
76
+ class Car (Driveable ):
77
+
78
+ def drive (self ):
79
+ print ("El cocche está en marcha" )
80
+
81
+ class Plane (Pilotable ):
82
+
83
+ def pilot (self ):
84
+ print ("El avión está volando" )
85
+
86
+ class Ship (Navigateable ):
87
+
88
+ def navigate (self ):
89
+ print ("El barco se encuentra navegando en alta mar" )
90
+
91
+
92
+ # Fin Rediseño para no violar el ISP
93
+
94
+ ## EJERCICIO EXTRA
95
+
96
+ class PrinterBW (ABC ):
97
+
98
+ @abstractmethod
99
+ def print_bn (self ):
100
+ pass
101
+
102
+ class PrinterColor (ABC ):
103
+
104
+ @abstractmethod
105
+ def print_color (self ):
106
+ pass
107
+
108
+ class Maileable (ABC ):
109
+
110
+ @abstractmethod
111
+ def send_mail (self ):
112
+ pass
113
+
114
+ class Faxeable (ABC ):
115
+
116
+ @abstractmethod
117
+ def send_fax (self ):
118
+ pass
119
+
120
+ class BwPrinter (PrinterBW ):
121
+
122
+ def print_bn (self ):
123
+ print ("Imprimiendo documento en Blanco/Negro" )
124
+
125
+ class ColorPrinter (PrinterColor ):
126
+
127
+ def print_color (self ):
128
+ print ("Imprimiendo documento en Color" )
129
+
130
+ class MultiFunction (PrinterBW , PrinterColor , Maileable , Faxeable ):
131
+
132
+ def print_bn (self ):
133
+ print ("Imprimiendo documento en Blanco/Negro" )
134
+
135
+ def print_color (self ):
136
+ print ("Imprimiendo documento en Color" )
137
+
138
+ def send_mail (
self ,
to = "[email protected] " ,
subject = "Test" ,
body = "This is a mail test" ):
139
+ print (f"Enviando correo electrónico a { to } con asunto { subject } y mensaje { body } " )
140
+
141
+ def send_fax (self , destination_number = "+345557890" , content = "This is a fax test" ):
142
+ print (f"Enviando fax a { destination_number } con contenido { content } " )
143
+
144
+
145
+ # Batería de pruebas
146
+
147
+ def test_bw_printer ():
148
+ printer = BwPrinter ()
149
+ printer .print_bn () # Debería imprimir en blanco y negro
150
+
151
+ def test_color_printer ():
152
+ printer = ColorPrinter ()
153
+ printer .print_color () # Debería imprimir en color
154
+
155
+ def test_multi_function_print_bn ():
156
+ printer = MultiFunction ()
157
+ printer .print_bn () # Debería imprimir en blanco y negro
158
+
159
+ def test_multi_function_print_color ():
160
+ printer = MultiFunction ()
161
+ printer .print_color () # Debería imprimir en color
162
+
163
+ def test_multi_function_send_mail ():
164
+ printer = MultiFunction ()
165
+ printer .
send_mail (
to = "[email protected] " ,
subject = "Prueba" ,
body = "Esto es una prueba" )
166
+
167
+ def test_multi_function_send_fax ():
168
+ printer = MultiFunction ()
169
+ printer .send_fax (destination_number = "+123456789" , content = "Esto es una prueba de fax" )
170
+
171
+ def test_no_extra_methods_bw_printer ():
172
+ printer = BwPrinter ()
173
+ try :
174
+ printer .print_color ()
175
+ except AttributeError :
176
+ print ("Error esperado: 'BwPrinter' no tiene método 'print_color'" )
177
+
178
+ def test_no_extra_methods_color_printer ():
179
+ printer = ColorPrinter ()
180
+ try :
181
+ printer .print_bn ()
182
+ except AttributeError :
183
+ print ("Error esperado: 'ColorPrinter' no tiene método 'print_bn'" )
184
+
185
+
186
+ print ("Pruebas de BwPrinter:" )
187
+ test_bw_printer ()
188
+ test_no_extra_methods_bw_printer ()
189
+
190
+ print ("\n Pruebas de ColorPrinter:" )
191
+ test_color_printer ()
192
+ test_no_extra_methods_color_printer ()
193
+
194
+ print ("\n Pruebas de MultiFunction:" )
195
+ test_multi_function_print_bn ()
196
+ test_multi_function_print_color ()
197
+ test_multi_function_send_mail ()
198
+ test_multi_function_send_fax ()
199
+
200
+
201
+ ## FIN EJERCICIO EXTRA
202
+
203
+
204
+
205
+
206
+
207
+ ### FIN Principio de Segregación de Interfaces (Interface Segregation Principle, ISP)
0 commit comments