Skip to content

Commit ff9c7f2

Browse files
authored
Merge pull request mouredev#7660 from davidrguez98/main
#29 - Python
2 parents 1dbc089 + b242f13 commit ff9c7f2

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
* DIFICULTAD EXTRA (opcional):
7+
* Crea un gestor de impresoras.
8+
* Requisitos:
9+
* 1. Algunas impresoras sólo imprimen en blanco y negro.
10+
* 2. Otras sólo a color.
11+
* 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
12+
* Instrucciones:
13+
* 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
14+
* 2. Aplica el ISP a la implementación.
15+
* 3. Desarrolla un código que compruebe que se cumple el principio.
16+
*/ """
17+
18+
from abc import ABC, abstractmethod
19+
20+
#EJERCICIO
21+
22+
"""
23+
Incorrecto
24+
"""
25+
26+
class WorkerInterface(ABC):
27+
28+
@abstractmethod
29+
def work(self):
30+
pass
31+
32+
@abstractmethod
33+
def eat(self):
34+
pass
35+
36+
class Human(WorkerInterface):
37+
38+
def work(self):
39+
print("Trabajando")
40+
41+
def eat(self):
42+
print("Comiendo")
43+
44+
class Robot(WorkerInterface):
45+
46+
def work(self):
47+
print("Trabajando")
48+
49+
def eat(self):
50+
pass
51+
52+
human = Human()
53+
human.work()
54+
human.eat()
55+
56+
robot = Robot()
57+
robot.work()
58+
robot.eat()
59+
60+
"""
61+
Correcto
62+
"""
63+
64+
class WorkInterface(ABC):
65+
66+
@abstractmethod
67+
def work(self):
68+
pass
69+
70+
class EatInterface(ABC):
71+
72+
@abstractmethod
73+
def eat(self):
74+
pass
75+
76+
class Human(WorkInterface, EatInterface):
77+
78+
def work(self):
79+
print("Trabajando")
80+
81+
def eat(self):
82+
print("Comiendo")
83+
84+
class Robot(WorkInterface):
85+
86+
def work(self):
87+
print("Trabajando")
88+
89+
human = Human()
90+
human.work()
91+
human.eat()
92+
93+
robot = Robot()
94+
robot.work()
95+
96+
#DIFICULTAD EXTRA
97+
98+
class BlackPrinter(ABC):
99+
100+
@abstractmethod
101+
def print(self):
102+
pass
103+
104+
class ColorPrinter(ABC):
105+
106+
@abstractmethod
107+
def print_color(self):
108+
pass
109+
110+
class ScanPrinter(ABC):
111+
112+
@abstractmethod
113+
def scan(self):
114+
pass
115+
116+
class FaxPrinter(ABC):
117+
118+
@abstractmethod
119+
def send_fax(self):
120+
pass
121+
122+
class Printer_one(BlackPrinter):
123+
124+
def print(self):
125+
print("Imprimiendo en blanco y negro.")
126+
127+
class Printer_two(BlackPrinter):
128+
129+
def print(self):
130+
print("Imprimiendo en blanco y negro.")
131+
132+
class Printer_three(ColorPrinter):
133+
134+
def print_color(self):
135+
print("Imprimiendo en color.")
136+
137+
class Printer_four(ColorPrinter):
138+
139+
def print_color(self):
140+
print("Imprimiendo en color.")
141+
142+
class Printer_Five(BlackPrinter, ColorPrinter, ScanPrinter, FaxPrinter):
143+
144+
def print(self):
145+
print("Imprimiendo en blanco y negro.")
146+
147+
def print_color(self):
148+
print("Imprimiendo en color.")
149+
150+
def scan(self):
151+
print("Escaneando.")
152+
153+
def send_fax(self):
154+
print("Enviando fax.")
155+
156+
class Printer_Six(BlackPrinter, ColorPrinter, ScanPrinter, FaxPrinter):
157+
158+
def print(self):
159+
print("Imprimiendo en blanco y negro.")
160+
161+
def print_color(self):
162+
print("Imprimiendo en color.")
163+
164+
def scan(self):
165+
print("Escaneando.")
166+
167+
def send_fax(self):
168+
print("Enviando fax.")
169+
170+
printer1 = Printer_one()
171+
printer1.print()
172+
173+
printer2 = Printer_two()
174+
printer2.print()
175+
176+
printer3 = Printer_three()
177+
printer3.print_color()
178+
179+
printer4 = Printer_four()
180+
printer4.print_color()
181+
182+
printer5 = Printer_Five()
183+
printer5.print()
184+
printer5.print_color()
185+
printer5.scan()
186+
printer5.send_fax()
187+
188+
printer6 = Printer_Six()
189+
printer6.print()
190+
printer6.print_color()
191+
printer6.scan()
192+
printer6.send_fax()

0 commit comments

Comments
 (0)