|
| 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 | +# To use interfaces |
| 19 | +from abc import ABC, abstractmethod |
| 20 | + |
| 21 | + |
| 22 | +# Incorrect |
| 23 | + |
| 24 | + |
| 25 | +class WorkerInterface(ABC): |
| 26 | + |
| 27 | + @abstractmethod |
| 28 | + def work(self): |
| 29 | + pass |
| 30 | + |
| 31 | + @abstractmethod |
| 32 | + def eat(self): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class Human(WorkerInterface): |
| 37 | + def work(self): |
| 38 | + print("Working") |
| 39 | + |
| 40 | + def eat(self): |
| 41 | + print("Eating") |
| 42 | + |
| 43 | + |
| 44 | +class Robot(WorkerInterface): |
| 45 | + def work(self): |
| 46 | + print("Working") |
| 47 | + |
| 48 | + def eat(self): |
| 49 | + pass # Robots don't eat |
| 50 | + |
| 51 | + |
| 52 | +""" What is wrong? |
| 53 | +- Interface has two methods, but one of its children classes doesn't use it. |
| 54 | +""" |
| 55 | + |
| 56 | + |
| 57 | +# Correct |
| 58 | + |
| 59 | + |
| 60 | +class WorkInterface(ABC): |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def work(self): |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +class EatInterface(ABC): |
| 68 | + |
| 69 | + @abstractmethod |
| 70 | + def eat(self): |
| 71 | + pass |
| 72 | + |
| 73 | + |
| 74 | +class Human(WorkInterface, EatInterface): |
| 75 | + def work(self): |
| 76 | + print("Working") |
| 77 | + |
| 78 | + def eat(self): |
| 79 | + print("Eating") |
| 80 | + |
| 81 | + |
| 82 | +class Robot(WorkInterface): |
| 83 | + def work(self): |
| 84 | + print("Working") |
| 85 | + |
| 86 | + |
| 87 | +human = Human() |
| 88 | +human.work() |
| 89 | +human.eat() |
| 90 | + |
| 91 | +robot = Robot() |
| 92 | +robot.work() |
| 93 | + |
| 94 | +""" |
| 95 | +Use only interfaces that are always necessary for the children. If one of the |
| 96 | +children doesn't use one of the methods, it's better to split the interface and |
| 97 | +add only the ones that are going to be implemented. |
| 98 | +""" |
| 99 | + |
| 100 | + |
| 101 | +""" |
| 102 | + * DIFICULTAD EXTRA (opcional): |
| 103 | + * Crea un gestor de impresoras. |
| 104 | + * Requisitos: |
| 105 | + * 1. Algunas impresoras sólo imprimen en blanco y negro. |
| 106 | + * 2. Otras sólo a color. |
| 107 | + * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax. |
| 108 | + * Instrucciones: |
| 109 | + * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones. |
| 110 | + * 2. Aplica el ISP a la implementación. |
| 111 | + * 3. Desarrolla un código que compruebe que se cumple el principio. |
| 112 | +""" |
| 113 | + |
| 114 | +# Define interfaces |
| 115 | + |
| 116 | + |
| 117 | +class PrinterInterface(ABC): |
| 118 | + |
| 119 | + @abstractmethod |
| 120 | + def print(self, document: str): |
| 121 | + pass |
| 122 | + |
| 123 | + |
| 124 | +class ColorPrinterInterface(ABC): |
| 125 | + |
| 126 | + @abstractmethod |
| 127 | + def print_color(self, document: str): |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +class ScannerInterface(ABC): |
| 132 | + |
| 133 | + @abstractmethod |
| 134 | + def scan(self, document: str) -> str: |
| 135 | + pass |
| 136 | + |
| 137 | + |
| 138 | +class FaxInterface(ABC): |
| 139 | + |
| 140 | + @abstractmethod |
| 141 | + def send_fax(self, document): |
| 142 | + pass |
| 143 | + |
| 144 | + |
| 145 | +# Create classes |
| 146 | + |
| 147 | + |
| 148 | +class Printer(PrinterInterface): |
| 149 | + def print(self, document: str): |
| 150 | + print(f"Printing (black & white): {document}") |
| 151 | + |
| 152 | + |
| 153 | +class ColorPrinter(ColorPrinterInterface): |
| 154 | + def print_color(self, document: str): |
| 155 | + print(f"Printing (color): {document}") |
| 156 | + |
| 157 | + |
| 158 | +class MultifunctionPrinter( |
| 159 | + PrinterInterface, ColorPrinterInterface, ScannerInterface, FaxInterface |
| 160 | +): |
| 161 | + def print(self, document: str): |
| 162 | + print(f"Printing (black & white): {document}") |
| 163 | + |
| 164 | + def print_color(self, document: str): |
| 165 | + print(f"Printing (color): {document}") |
| 166 | + |
| 167 | + def scan(self, document: str) -> str: |
| 168 | + print(f"Scanning document: {document}") |
| 169 | + return "scanned" + document |
| 170 | + |
| 171 | + def send_fax(self, document): |
| 172 | + print(f"Sending through fax: {document}") |
| 173 | + |
| 174 | + |
| 175 | +# Testing the classes |
| 176 | + |
| 177 | +DOCUMENT = "my-document.txt" |
| 178 | + |
| 179 | +printer = Printer() |
| 180 | +printer.print(DOCUMENT) |
| 181 | + |
| 182 | +color_printer = ColorPrinter() |
| 183 | +color_printer.print_color(DOCUMENT) |
| 184 | + |
| 185 | +multifunction_printer = MultifunctionPrinter() |
| 186 | +multifunction_printer.print(DOCUMENT) |
| 187 | +multifunction_printer.print_color(DOCUMENT) |
| 188 | +multifunction_printer.scan(DOCUMENT) |
| 189 | +multifunction_printer.send_fax(DOCUMENT) |
0 commit comments