|
| 1 | +# 29 SOLID: PRINCIPIO DE SEGREGACIÓN DE INTERFACES (ISP) |
| 2 | +# Ejercicio |
| 3 | +# Ejemplo Pato sin ISP |
| 4 | +from abc import ABC, abstractmethod |
| 5 | + |
| 6 | + |
| 7 | +class Pato (ABC): |
| 8 | + def __init__(self, tipo): |
| 9 | + self.tipo = tipo |
| 10 | + |
| 11 | + @abstractmethod |
| 12 | + def vuela(self): |
| 13 | + pass |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def nada(self): |
| 17 | + pass |
| 18 | + |
| 19 | + @abstractmethod |
| 20 | + def dice(self): |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +class PatoSalvaje(Pato): |
| 25 | + def __init__(self): |
| 26 | + super().__init__("salvaje") |
| 27 | + |
| 28 | + def vuela(self): |
| 29 | + print("El pato salvaje vuela.") |
| 30 | + |
| 31 | + def nada(self): |
| 32 | + print("El pato salvaje nada.") |
| 33 | + |
| 34 | + def dice(self): |
| 35 | + print("El pato salvaje dice Quack.") |
| 36 | + |
| 37 | + |
| 38 | +class PatoDomestico(Pato): |
| 39 | + def __init__(self): |
| 40 | + super().__init__("domestico") |
| 41 | + |
| 42 | + def vuela(self): |
| 43 | + print("El pato doméstico vuela.") |
| 44 | + |
| 45 | + def nada(self): |
| 46 | + print("El pato doméstico nada.") |
| 47 | + |
| 48 | + def dice(self): |
| 49 | + print("El pato doméstico dice Quack.") |
| 50 | + |
| 51 | + |
| 52 | +class PatoPlastico(Pato): |
| 53 | + def __init__(self): |
| 54 | + super().__init__("plastico") |
| 55 | + |
| 56 | + def vuela(self): |
| 57 | + print("El pato de plástico no puede volar.") |
| 58 | + |
| 59 | + def nada(self): |
| 60 | + print("El pato de plástico no puede nadar.") |
| 61 | + |
| 62 | + def dice(self): |
| 63 | + print("El pato de plástico dice Squeak.") |
| 64 | + |
| 65 | + |
| 66 | +# Uso |
| 67 | +print("Con LSP") |
| 68 | +pato1 = PatoSalvaje() |
| 69 | +pato2 = PatoDomestico() |
| 70 | +pato3 = PatoPlastico() |
| 71 | + |
| 72 | +pato1.vuela() |
| 73 | +pato1.nada() |
| 74 | +pato1.dice() |
| 75 | + |
| 76 | +pato2.vuela() |
| 77 | +pato2.nada() |
| 78 | +pato2.dice() |
| 79 | + |
| 80 | +pato3.vuela() |
| 81 | +pato3.nada() |
| 82 | +pato3.dice() |
| 83 | + |
| 84 | + |
| 85 | +# Ejemplo Pato con ISP |
| 86 | +class Volador (ABC): |
| 87 | + @abstractmethod |
| 88 | + def vuela(self): |
| 89 | + pass |
| 90 | + |
| 91 | + |
| 92 | +class Nadador (ABC): |
| 93 | + @abstractmethod |
| 94 | + def nada(self): |
| 95 | + pass |
| 96 | + |
| 97 | + |
| 98 | +class Hablador (ABC): |
| 99 | + @abstractmethod |
| 100 | + def dice(self): |
| 101 | + pass |
| 102 | + |
| 103 | + |
| 104 | +class Flotador (ABC): |
| 105 | + @abstractmethod |
| 106 | + def flota(self): |
| 107 | + pass |
| 108 | + |
| 109 | + |
| 110 | +class PatoSalvaje(Volador, Nadador, Hablador): |
| 111 | + def __init__(self, tipo): |
| 112 | + self.tipo = tipo |
| 113 | + |
| 114 | + def vuela(self): |
| 115 | + print(f"El {self.tipo} vuela.") |
| 116 | + |
| 117 | + def nada(self): |
| 118 | + print(f"El {self.tipo} nada.") |
| 119 | + |
| 120 | + def dice(self): |
| 121 | + print(f"El {self.tipo} dice Quack.") |
| 122 | + |
| 123 | + |
| 124 | +class PatoDomestico(Nadador, Hablador): |
| 125 | + def __init__(self, tipo): |
| 126 | + self.tipo = tipo |
| 127 | + |
| 128 | + def nada(self): |
| 129 | + print(f"El {self.tipo} nada.") |
| 130 | + |
| 131 | + def dice(self): |
| 132 | + print(f"El {self.tipo} dice Quack.") |
| 133 | + |
| 134 | + |
| 135 | +class PatoPlastico(Hablador, Flotador): |
| 136 | + def __init__(self, tipo): |
| 137 | + self.tipo = tipo |
| 138 | + |
| 139 | + def dice(self): |
| 140 | + print(f"El {self.tipo} dice Quick.") |
| 141 | + |
| 142 | + def flota(self): |
| 143 | + print(f"El {self.tipo} flota en la bañera.") |
| 144 | + |
| 145 | + |
| 146 | +# Uso |
| 147 | +print("Con ISP") |
| 148 | +pato4 = PatoSalvaje("Pato Salvaje") |
| 149 | +pato5 = PatoDomestico("Pato Domestico") |
| 150 | +pato6 = PatoPlastico("Pato de plastico") |
| 151 | + |
| 152 | +pato4.vuela() |
| 153 | +pato4.nada() |
| 154 | +pato4.dice() |
| 155 | + |
| 156 | +pato5.nada() |
| 157 | +pato5.dice() |
| 158 | + |
| 159 | +pato6.dice() |
| 160 | +pato6.flota() |
| 161 | + |
| 162 | +# Extra |
| 163 | + |
| 164 | + |
| 165 | +class Imprimir (ABC): |
| 166 | + @abstractmethod |
| 167 | + def imprime(self): |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | +class Escanear (ABC): |
| 172 | + @abstractmethod |
| 173 | + def escanea(self): |
| 174 | + pass |
| 175 | + |
| 176 | + |
| 177 | +class Enviar_Fax (ABC): |
| 178 | + @abstractmethod |
| 179 | + def envia(self): |
| 180 | + pass |
| 181 | + |
| 182 | + |
| 183 | +class Impresora_ByN(Imprimir): |
| 184 | + def __init__(self, tipo): |
| 185 | + self.tipo = tipo |
| 186 | + |
| 187 | + def imprime(self): |
| 188 | + print(f"Se {self.imprime.__name__} documento en {self.tipo}.") |
| 189 | + |
| 190 | + |
| 191 | +class Impresora_Color(Imprimir): |
| 192 | + def __init__(self, tipo): |
| 193 | + self.tipo = tipo |
| 194 | + |
| 195 | + def imprime(self): |
| 196 | + print(f"Se {self.imprime.__name__} documento en {self.tipo}.") |
| 197 | + |
| 198 | + |
| 199 | +class Impresora_Multifuncion(Imprimir, Escanear, Enviar_Fax): |
| 200 | + def __init__(self, tipo): |
| 201 | + self.tipo = tipo |
| 202 | + |
| 203 | + def imprime(self): |
| 204 | + print(f"Se {self.imprime.__name__} documento en {self.tipo}.") |
| 205 | + |
| 206 | + def escanea(self): |
| 207 | + print(f"Se {self.escanea.__name__} documento en {self.tipo}.") |
| 208 | + |
| 209 | + def envia(self): |
| 210 | + print(f"Se {self.envia.__name__} fax en {self.tipo}.") |
| 211 | + |
| 212 | +# Gestor de impresoras |
| 213 | + |
| 214 | + |
| 215 | +class GestorImpresoras: |
| 216 | + def __init__(self): |
| 217 | + self.impresoras = [] |
| 218 | + |
| 219 | + def agregar_impresora(self, impresora): |
| 220 | + self.impresoras.append(impresora) |
| 221 | + |
| 222 | + def listar_impresoras(self): |
| 223 | + print("\nListado de impresoras disponibles:") |
| 224 | + for id, impresora in enumerate(self.impresoras, start=1): |
| 225 | + print(f"{id}. {type(impresora).__name__} - {impresora.tipo}") |
| 226 | + |
| 227 | + def seleccionar_impresora(self): |
| 228 | + self.listar_impresoras() |
| 229 | + seleccion = int( |
| 230 | + input("Seleccione el número de la impresora que desea usar: ")) |
| 231 | + if seleccion <= 0 or seleccion > len(self.impresoras): |
| 232 | + print("Selección inválida.") |
| 233 | + return None |
| 234 | + return self.impresoras[seleccion - 1] |
| 235 | + |
| 236 | + def imprimir_documento(self, impresora): |
| 237 | + if isinstance(impresora, Imprimir): |
| 238 | + impresora.imprime() |
| 239 | + else: |
| 240 | + print(f"La impresora { |
| 241 | + type(impresora).__name__} no puede imprimir.") |
| 242 | + |
| 243 | + def escanear_documento(self, impresora): |
| 244 | + if isinstance(impresora, Escanear): |
| 245 | + impresora.escanea() |
| 246 | + else: |
| 247 | + print(f"La impresora { |
| 248 | + type(impresora).__name__} no puede escanear.") |
| 249 | + |
| 250 | + def enviar_documento(self, impresora): |
| 251 | + if isinstance(impresora, Enviar_Fax): |
| 252 | + impresora.envia() |
| 253 | + else: |
| 254 | + print(f"La impresora { |
| 255 | + type(impresora).__name__} no puede enviar fax.") |
| 256 | + |
| 257 | + |
| 258 | +# Uso del gestor de impresoras |
| 259 | +if __name__ == "__main__": |
| 260 | + gestor = GestorImpresoras() |
| 261 | + |
| 262 | + # Agregar impresoras |
| 263 | + impresora1 = Impresora_ByN("Impresora B/N 1") |
| 264 | + impresora2 = Impresora_Color("Impresora Color 1") |
| 265 | + impresora3 = Impresora_Multifuncion("Impresora Multifunción 1") |
| 266 | + impresora4 = Impresora_ByN("Impresora B/N Secretaría") |
| 267 | + |
| 268 | + gestor.agregar_impresora(impresora1) |
| 269 | + gestor.agregar_impresora(impresora2) |
| 270 | + gestor.agregar_impresora(impresora3) |
| 271 | + gestor.agregar_impresora(impresora4) |
| 272 | + |
| 273 | + gestor.listar_impresoras() |
| 274 | + gestor.seleccionar_impresora() |
| 275 | + |
| 276 | + gestor.imprimir_documento(impresora4) |
| 277 | + gestor.escanear_documento(impresora3) |
| 278 | + gestor.enviar_documento(impresora3) |
| 279 | + |
| 280 | +""" |
| 281 | +#Interacción |
| 282 | + while True: |
| 283 | + print("\n1. Agregar impresora") |
| 284 | + print("2. Visualizar listado de impresoras") |
| 285 | + print("3. Imprimir documento") |
| 286 | + print("4. Escanear documento") |
| 287 | + print("5. Enviar Fax") |
| 288 | + print("6. Salir") |
| 289 | + opcion = input("Seleccione una opción: ") |
| 290 | +
|
| 291 | + if opcion == "1": |
| 292 | + tipo = input("Ingrese el tipo de impresora (ByN/Color/Multifuncion): ").lower() |
| 293 | + nombre = input("Ingrese el nombre de la impresora: ") |
| 294 | + if tipo == "byn": |
| 295 | + gestor.agregar_impresora(Impresora_ByN(nombre)) |
| 296 | + elif tipo == "color": |
| 297 | + gestor.agregar_impresora(Impresora_Color(nombre)) |
| 298 | + elif tipo == "multifuncion": |
| 299 | + gestor.agregar_impresora(Impresora_Multifuncion(nombre)) |
| 300 | + else: |
| 301 | + print("Tipo de impresora no reconocido.") |
| 302 | +
|
| 303 | + elif opcion == "2": |
| 304 | + gestor.listar_impresoras() |
| 305 | +
|
| 306 | + elif opcion == "3": |
| 307 | + seleccionada = gestor.seleccionar_impresora() |
| 308 | + if seleccionada: |
| 309 | + gestor.imprimir_documento(seleccionada) |
| 310 | +
|
| 311 | + elif opcion == "4": |
| 312 | + seleccionada = gestor.seleccionar_impresora() |
| 313 | + if seleccionada: |
| 314 | + gestor.escanear_documento(seleccionada) |
| 315 | +
|
| 316 | + elif opcion == "5": |
| 317 | + seleccionada = gestor.seleccionar_impresora() |
| 318 | + if seleccionada: |
| 319 | + gestor.enviar_documento(seleccionada) |
| 320 | +
|
| 321 | + elif opcion == "6": |
| 322 | + print("Saliendo del programa...") |
| 323 | + break |
| 324 | +
|
| 325 | + else: |
| 326 | + print("Opción inválida. Intente nuevamente.") |
| 327 | +
|
| 328 | + """ |
0 commit comments