|
| 1 | +""" |
| 2 | +/* |
| 3 | + * EJERCICIO: |
| 4 | + * Explora el "Principio SOLID de Segregación de Interfaces |
| 5 | + * (Interface Segregation Principle, ISP)", y crea un ejemplo |
| 6 | + * simple donde se muestre su funcionamiento de forma correcta e incorrecta. |
| 7 | + * |
| 8 | + * DIFICULTAD EXTRA (opcional): |
| 9 | + * Crea un gestor de impresoras. |
| 10 | + * Requisitos: |
| 11 | + * 1. Algunas impresoras sólo imprimen en blanco y negro. |
| 12 | + * 2. Otras sólo a color. |
| 13 | + * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax. |
| 14 | + * Instrucciones: |
| 15 | + * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones. |
| 16 | + * 2. Aplica el ISP a la implementación. |
| 17 | + * 3. Desarrolla un código que compruebe que se cumple el principio. |
| 18 | + */ |
| 19 | +""" |
| 20 | + |
| 21 | +from abc import ABC, abstractmethod |
| 22 | + #Incorrecto |
| 23 | +class VehicleInterface(ABC): |
| 24 | + @abstractmethod |
| 25 | + def start_engine(self): |
| 26 | + pass |
| 27 | + |
| 28 | + @abstractmethod |
| 29 | + def stop_engine(self): |
| 30 | + pass |
| 31 | + |
| 32 | + @abstractmethod |
| 33 | + def fly(self): |
| 34 | + pass |
| 35 | + |
| 36 | +class Car(VehicleInterface): |
| 37 | + |
| 38 | + def start_engine(self): |
| 39 | + print("Car engine started.") |
| 40 | + |
| 41 | + def stop_engine(self): |
| 42 | + print("Car engine stopped.") |
| 43 | + |
| 44 | + def fly(self): |
| 45 | + #los autos no vuelan |
| 46 | + pass |
| 47 | + |
| 48 | +class airplanes(VehicleInterface): |
| 49 | + |
| 50 | + def start_engine(self): |
| 51 | + print("airplanes engine started.") |
| 52 | + |
| 53 | + def stop_engine(self): |
| 54 | + print("airplanes engine stopped.") |
| 55 | + |
| 56 | + def fly(self): |
| 57 | + print("airplanes is flying") |
| 58 | + |
| 59 | + #Correcto |
| 60 | +class VehicleInterface(ABC): |
| 61 | + @abstractmethod |
| 62 | + def start_engine(self): |
| 63 | + pass |
| 64 | + |
| 65 | + @abstractmethod |
| 66 | + def stop_engine(self): |
| 67 | + pass |
| 68 | + |
| 69 | +class FlyInterface(VehicleInterface): |
| 70 | + @abstractmethod |
| 71 | + def fly(self): |
| 72 | + pass |
| 73 | + |
| 74 | +class Car(VehicleInterface): |
| 75 | + |
| 76 | + def start_engine(self): |
| 77 | + print("Car engine started.") |
| 78 | + |
| 79 | + def stop_engine(self): |
| 80 | + print("Car engine stopped.") |
| 81 | + |
| 82 | + |
| 83 | +class airplanes(FlyInterface, VehicleInterface): |
| 84 | + |
| 85 | + def start_engine(self): |
| 86 | + print("airplanes engine started.") |
| 87 | + |
| 88 | + def stop_engine(self): |
| 89 | + print("airplanes engine stopped.") |
| 90 | + |
| 91 | + def fly(self): |
| 92 | + print("airplanes is flying") |
| 93 | + |
| 94 | +#Extra |
| 95 | +class PrinterIsBlackInterface(ABC): |
| 96 | + @abstractmethod |
| 97 | + def printer_black(self, document): |
| 98 | + pass |
| 99 | + |
| 100 | +class PrinterIsColorInterface(ABC): |
| 101 | + @abstractmethod |
| 102 | + def printer_color(self, document): |
| 103 | + pass |
| 104 | + |
| 105 | +class PrinterScanInterface(ABC): |
| 106 | + @abstractmethod |
| 107 | + def printer_scan(self, document): |
| 108 | + pass |
| 109 | + |
| 110 | +class PrinterFaxInterface(ABC): |
| 111 | + @abstractmethod |
| 112 | + def send_fax(self, document): |
| 113 | + pass |
| 114 | + |
| 115 | +class Printer(PrinterIsBlackInterface): |
| 116 | + def printer_black(self, document): |
| 117 | + print(f"imprimiendo en blanco y negro {document}") |
| 118 | + |
| 119 | +class PrinterColor(PrinterIsColorInterface): |
| 120 | + def printer_color(self, document): |
| 121 | + print(f"imprimiendo a color {document}") |
| 122 | + |
| 123 | +class PrinterMultiFunction(PrinterIsBlackInterface, PrinterIsColorInterface, PrinterScanInterface, PrinterFaxInterface): |
| 124 | + def printer_black(self, document): |
| 125 | + print(f"imprimiendo en blanco y negro {document}") |
| 126 | + |
| 127 | + def printer_color(self, document): |
| 128 | + print(f"imprimiendo a color {document}") |
| 129 | + |
| 130 | + def printer_scan(self, document): |
| 131 | + print(f"escaneando documento {document}") |
| 132 | + |
| 133 | + def send_fax(self, document): |
| 134 | + print(f"enviando {document} por fax") |
| 135 | + |
| 136 | + |
| 137 | +printed = Printer() |
| 138 | +printed.printer_black("documento.ppt") |
| 139 | + |
| 140 | +printed_color = PrinterColor() |
| 141 | +printed_color.printer_color("documento.ppt") |
| 142 | + |
| 143 | +printed_multifunction = PrinterMultiFunction() |
| 144 | +printed_multifunction.printer_black("documento.ppt") |
| 145 | +printed_multifunction.printer_color("documento.ppt") |
| 146 | +printed_multifunction.printer_scan("documento.ppt") |
| 147 | +printed_multifunction.send_fax("documento.ppt") |
0 commit comments