|
| 1 | +""" |
| 2 | +/* |
| 3 | + * EJERCICIO: |
| 4 | + * Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)" |
| 5 | + * y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta. |
| 6 | + * |
| 7 | +""" |
| 8 | +from abc import ABC, abstractmethod |
| 9 | + |
| 10 | +# Forma incorrecta |
| 11 | + |
| 12 | + |
| 13 | +class Bird(ABC): |
| 14 | + |
| 15 | + def __init__(self, name): |
| 16 | + self.name = name |
| 17 | + |
| 18 | + @abstractmethod |
| 19 | + def fly(self): |
| 20 | + pass |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + def swim(self): |
| 24 | + pass |
| 25 | + |
| 26 | + @abstractmethod |
| 27 | + def do_sound(self) -> str: |
| 28 | + pass |
| 29 | + |
| 30 | + |
| 31 | +class Crow(Bird): |
| 32 | + |
| 33 | + def fly(self): |
| 34 | + print(f"{self.name} is flying high and fast!") |
| 35 | + |
| 36 | + def swim(self): |
| 37 | + raise NotImplementedError("Crows don't swim!") |
| 38 | + |
| 39 | + def do_sound(self) -> str: |
| 40 | + return "Caw" |
| 41 | + |
| 42 | + |
| 43 | +class Duck(Bird): |
| 44 | + |
| 45 | + def fly(self): |
| 46 | + print(f"{self.name} is flying not very high") |
| 47 | + |
| 48 | + def swim(self): |
| 49 | + print(f"{self.name} swims in the lake and quacks") |
| 50 | + |
| 51 | + def do_sound(self) -> str: |
| 52 | + return "Quack" |
| 53 | + |
| 54 | +# Forma correcta |
| 55 | + |
| 56 | + |
| 57 | +class Bird(ABC): |
| 58 | + |
| 59 | + def __init__(self, name): |
| 60 | + self.name = name |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def do_sound(self) -> str: |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +class FlyingBird(Bird): |
| 68 | + |
| 69 | + @abstractmethod |
| 70 | + def fly(self): |
| 71 | + pass |
| 72 | + |
| 73 | + |
| 74 | +class SwimmingBird(Bird): |
| 75 | + |
| 76 | + @abstractmethod |
| 77 | + def swim(self): |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +class Crow(FlyingBird): |
| 82 | + |
| 83 | + def fly(self): |
| 84 | + print(f"{self.name} is flying high and fast!") |
| 85 | + |
| 86 | + def do_sound(self) -> str: |
| 87 | + return "Caw" |
| 88 | + |
| 89 | + |
| 90 | +class Duck(SwimmingBird, FlyingBird): |
| 91 | + |
| 92 | + def fly(self): |
| 93 | + print(f"{self.name} is flying not very high") |
| 94 | + |
| 95 | + def swim(self): |
| 96 | + print(f"{self.name} swims in the lake and quacks") |
| 97 | + |
| 98 | + def do_sound(self) -> str: |
| 99 | + return "Quack" |
| 100 | + |
| 101 | + |
| 102 | +""" |
| 103 | + * DIFICULTAD EXTRA (opcional): |
| 104 | + * Crea un gestor de impresoras. |
| 105 | + * Requisitos: |
| 106 | + * 1. Algunas impresoras sólo imprimen en blanco y negro. |
| 107 | + * 2. Otras sólo a color. |
| 108 | + * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax. |
| 109 | + * Instrucciones: |
| 110 | + * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones. |
| 111 | + * 2. Aplica el ISP a la implementación. |
| 112 | + * 3. Desarrolla un código que compruebe que se cumple el principio. |
| 113 | + */ |
| 114 | +""" |
0 commit comments