|
| 1 | +""" |
| 2 | + * EJERCICIO: |
| 3 | + * Explora el "Principio SOLID de Segregación de Interfaces |
| 4 | + * (Interface Segregation Principle, ISP)", y crea un ejemplo |
| 5 | + * simple donde se muestre su funcionamiento de forma correcta e incorrecta. |
| 6 | +""" |
| 7 | +#INCORRECTO |
| 8 | +from abc import ABC, abstractmethod |
| 9 | + |
| 10 | +class Worker(): |
| 11 | + def work(self): |
| 12 | + print("Estoy trabajando") |
| 13 | + |
| 14 | + def eat(self): |
| 15 | + print("Estoy comiendo") |
| 16 | + |
| 17 | +class Human_1(Worker): |
| 18 | + def work(self): |
| 19 | + print("Estoy trabajando") |
| 20 | + |
| 21 | + def eat(self): |
| 22 | + print("Estoy comiendo") |
| 23 | + |
| 24 | +class Robot_1(Worker): |
| 25 | + def work(self): |
| 26 | + print("Estoy trabajando") |
| 27 | + |
| 28 | +my_human_1 = Human_1() |
| 29 | +my_robot_1 = Robot_1() |
| 30 | + |
| 31 | +my_human_1.work() |
| 32 | +my_human_1.eat() |
| 33 | + |
| 34 | +#my_robot_1.eat() Robot() no tiene definido una función eat() por tanto no debería poder implementarla. Los robots no comen. |
| 35 | + |
| 36 | +my_robot_1.work() |
| 37 | + |
| 38 | +#CORRECTO |
| 39 | +class Work(ABC): |
| 40 | + @abstractmethod |
| 41 | + def work(self): |
| 42 | + print("Estoy trabajando") |
| 43 | + |
| 44 | +class Eat(): |
| 45 | + @abstractmethod |
| 46 | + def eat(self): |
| 47 | + print("Estoy comiendo") |
| 48 | + |
| 49 | +class Human(Work,Eat): |
| 50 | + def work(self): |
| 51 | + print("Estoy trabajando") |
| 52 | + |
| 53 | + def eat(self): |
| 54 | + print("Estoy comiendo") |
| 55 | + |
| 56 | +class Robot(Work): |
| 57 | + def work(self): |
| 58 | + print("Estoy trabajando") |
| 59 | + |
| 60 | +my_human = Human() |
| 61 | +my_robot = Robot() |
| 62 | + |
| 63 | +my_human.work() |
| 64 | +my_human.eat() |
| 65 | + |
| 66 | +my_robot.work() |
| 67 | +#my_robot.eat() Ahora Robot() no tiene definida ninguna función eat() al solo heredar de Work(), ya que los robots no comen. |
| 68 | + |
| 69 | +""" |
| 70 | + * DIFICULTAD EXTRA (opcional): |
| 71 | + * Crea un gestor de impresoras. |
| 72 | + * Requisitos: |
| 73 | + * 1. Algunas impresoras sólo imprimen en blanco y negro. |
| 74 | + * 2. Otras sólo a color. |
| 75 | + * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax. |
| 76 | + * Instrucciones: |
| 77 | + * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones. |
| 78 | + * 2. Aplica el ISP a la implementación. |
| 79 | + * 3. Desarrolla un código que compruebe que se cumple el principio. |
| 80 | +""" |
| 81 | + |
| 82 | +class Printer(ABC): |
| 83 | + @abstractmethod |
| 84 | + def __init__(self,type): |
| 85 | + self.type = type |
| 86 | + |
| 87 | + @abstractmethod |
| 88 | + def printer(self,document): |
| 89 | + print(f"Imprimiento en {self.type} el documento:\n{document}") |
| 90 | + |
| 91 | +class Scanner(ABC): |
| 92 | + @abstractmethod |
| 93 | + def scan(self,document): |
| 94 | + print(f"Escaneando el documento: {document}") |
| 95 | + |
| 96 | +class Fax(ABC): |
| 97 | + @abstractmethod |
| 98 | + def send_fax(self,document): |
| 99 | + print(f"Enviando por fax el documento: {document}") |
| 100 | + |
| 101 | +class Regular_Printer(Printer): |
| 102 | + def __init__(self): |
| 103 | + self.type = "Blanco y Negro" |
| 104 | + |
| 105 | + def printer(self,document): |
| 106 | + print(f"Imprimiendo en {self.type} el documento:\n{document}\n") |
| 107 | + |
| 108 | +class Color_Printer(Printer): |
| 109 | + def __init__(self): |
| 110 | + self.type = "Color" |
| 111 | + |
| 112 | + def printer(self,document): |
| 113 | + print(f"Imprimiendo en {self.type} el documento:\n{document}\n") |
| 114 | + |
| 115 | +class Multifunction_Printer(Printer,Scanner,Fax): |
| 116 | + def __init__(self): |
| 117 | + self.type = "Color" |
| 118 | + |
| 119 | + def printer(self,document): |
| 120 | + print(f"Imprimiendo en {self.type} el documento:\n{document}\n") |
| 121 | + |
| 122 | + def scan (self,document): |
| 123 | + print(f"Escaneando documento {document}") |
| 124 | + |
| 125 | + def send_fax(self,document): |
| 126 | + print(f"Enviando el documento {document} por fax") |
| 127 | + |
| 128 | +my_regular_printer = Regular_Printer() |
| 129 | +my_color_printer = Color_Printer() |
| 130 | +my_multifunction_printer = Multifunction_Printer() |
| 131 | + |
| 132 | +my_regular_printer.printer("Me llamo Alex") |
| 133 | +my_color_printer.printer("Me llamo Alex") |
| 134 | +my_multifunction_printer.printer("Me llamo Alex") |
| 135 | +my_multifunction_printer.scan("DNI de Alex") |
| 136 | +my_multifunction_printer.send_fax("Nómina de Alex") |
0 commit comments