Skip to content

Commit 1b763bf

Browse files
authored
Merge pull request #8375 from ignaciovihe/ejercicio-29
#29 - Python
2 parents a0dd160 + a1186c7 commit 1b763bf

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""
2+
* EJERCICIO:
3+
* Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)"
4+
* y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta.
5+
"""
6+
7+
from abc import ABC, abstractmethod
8+
#ISP incorrecto
9+
10+
class Animal: # La superclase obliga a implementar todos los metodos, aunque alguno no tiene sentido para la subclase perro.
11+
def fly(self):
12+
pass
13+
14+
def run(self):
15+
pass
16+
17+
def swim(self):
18+
pass
19+
20+
21+
class Dog(Animal):
22+
def fly(self):
23+
raise Exception("Los perros no vuelan.")
24+
25+
def run(self):
26+
print("El perro corre")
27+
28+
def swimm(self):
29+
print("El perro nada")
30+
31+
"""my_dog = Dog()
32+
my_dog.run()
33+
my_dog.swimm()
34+
my_dog.fly()""" #Da error
35+
36+
# ISP correcto
37+
class RunInterface(ABC):
38+
@abstractmethod
39+
def run(self):
40+
pass
41+
42+
class FLyInterface(ABC):
43+
@abstractmethod
44+
def fly(self):
45+
pass
46+
47+
class SwimInterface(ABC):
48+
@abstractmethod
49+
def swim(self):
50+
pass
51+
52+
class Dog(RunInterface,SwimInterface):
53+
def run(self):
54+
print("El perro corre")
55+
56+
def swim(self):
57+
print("El perro nada")
58+
59+
class Bird(FLyInterface):
60+
def fly(self):
61+
print("El pajaro vuela")
62+
63+
my_dog = Dog()
64+
my_dog.run()
65+
66+
my_bird = Bird()
67+
my_bird.fly()
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+
class MonochromePrinterInterface(ABC):
83+
@abstractmethod
84+
def print_monochrome(self, document: str):
85+
pass
86+
87+
88+
class ColorPrinterInterface(ABC):
89+
@abstractmethod
90+
def print_color(self, document: str):
91+
pass
92+
93+
94+
class ScannInterface(ABC):
95+
@abstractmethod
96+
def scan(self, document) -> str:
97+
pass
98+
99+
100+
class FaxInterface(ABC):
101+
@abstractmethod
102+
def fax(self, document: str):
103+
pass
104+
105+
106+
class MonochromePrinter(MonochromePrinterInterface):
107+
def print_monochrome(self, document):
108+
print(f"Imprimiendo {document} en blanco y negro")
109+
110+
111+
class ColorPrinter(ColorPrinterInterface):
112+
def print_color(self, document):
113+
print(f"Imprimiendo {document} en color")
114+
115+
116+
class MultifunctionPrinter(MonochromePrinterInterface, ColorPrinterInterface, ScannInterface, FaxInterface):
117+
def fax(self, document):
118+
print(f"Enviando {document} por fax")
119+
120+
def scan(self, document):
121+
print(f"Escaneando documento {document}")
122+
return f"Documento {document} escaneado"
123+
124+
def print_color(self, document):
125+
print(f"Imprimiendo {document} en color")
126+
127+
def print_monochrome(self, document):
128+
print(f"Imprimiendo {document} en blanco y negro")
129+
130+
131+
monochrome_printer = MonochromePrinter()
132+
color_printer = ColorPrinter()
133+
multifunction = MultifunctionPrinter()
134+
135+
monochrome_printer.print_monochrome("doc1.pdf")
136+
color_printer.print_color("doc1.pdf")
137+
multifunction.fax("doc1.pdf")
138+
multifunction.print_monochrome("doc1.pdf")
139+
multifunction.print_color("doc1.pdf")
140+
multifunction.scan("doc1.pdf")

0 commit comments

Comments
 (0)