|
| 1 | +# ╔═════════════════════════════════════╗ |
| 2 | +# ║ Autor: Kenys Alvarado ║ |
| 3 | +# ║ GitHub: https://github.com/Kenysdev ║ |
| 4 | +# ║ 2024 - Python ║ |
| 5 | +# ╚═════════════════════════════════════╝ |
| 6 | + |
| 7 | +# ------------------------------------------------- |
| 8 | +# * SOLID: PRINCIPIO DE SUSTITUCIÓN DE LISKOV (LSP) |
| 9 | +# ------------------------------------------------- |
| 10 | + |
| 11 | +# mas info: |
| 12 | +# https://blog.nonstopio.com/liskov-substitution-principle-in-python-b68d62924f7b |
| 13 | + |
| 14 | +# _________________ |
| 15 | +from abc import ABC, abstractmethod |
| 16 | + |
| 17 | +# Clase base, con contrato: |
| 18 | +class Animal(ABC): |
| 19 | + def __init__(self, name: str): |
| 20 | + self.name = name |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + def make_sound(self) -> str: |
| 24 | + pass |
| 25 | + |
| 26 | +# _________________ |
| 27 | +# clases derivadas: |
| 28 | +class Dog(Animal): |
| 29 | + def make_sound(self) -> str: |
| 30 | + return (f"{self.name} hace Woof") |
| 31 | + |
| 32 | +class Cat(Animal): |
| 33 | + def make_sound(self) -> str: |
| 34 | + return (f"{self.name} hace Meow") |
| 35 | + |
| 36 | +# _________________ |
| 37 | +dog = Dog("Max") |
| 38 | +print(dog.make_sound()) |
| 39 | + |
| 40 | +cat = Cat("Milo") |
| 41 | +print(cat.make_sound()) |
| 42 | + |
| 43 | + |
| 44 | +""" |
| 45 | +* EJERCICIO |
| 46 | +* Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como |
| 47 | +* cumplir el LSP. |
| 48 | +* Instrucciones: |
| 49 | +* 1. Crea la clase Vehículo. |
| 50 | +* 2. Añade tres subclases de Vehículo. |
| 51 | +* 3. Implementa las operaciones "acelerar" y "frenar" como corresponda. |
| 52 | +* 4. Desarrolla un código que compruebe que se cumple el LSP. |
| 53 | +""" |
| 54 | + |
| 55 | +# _________________ |
| 56 | +# Clase base abstracta |
| 57 | +class Vehicle(ABC): |
| 58 | + def __init__(self, brand: str, model: str): |
| 59 | + self._brand = brand |
| 60 | + self._model = model |
| 61 | + |
| 62 | + @property |
| 63 | + def brand(self) -> str: |
| 64 | + return self._brand |
| 65 | + |
| 66 | + @property |
| 67 | + def model(self) -> str: |
| 68 | + return self._model |
| 69 | + |
| 70 | + @abstractmethod |
| 71 | + def accelerate(self): |
| 72 | + pass |
| 73 | + |
| 74 | + @abstractmethod |
| 75 | + def brake(self): |
| 76 | + pass |
| 77 | + |
| 78 | +# _________________ |
| 79 | +# clases derivadas: |
| 80 | +class Car(Vehicle): |
| 81 | + def accelerate(self) -> str: |
| 82 | + propertys: str = f"{self.brand} - {self.model}" |
| 83 | + return (f"Acelerando auto: {propertys}") |
| 84 | + |
| 85 | + def brake(self) -> str: |
| 86 | + propertys: str = f"{self.brand} - {self.model}" |
| 87 | + return (f"Frenando auto: {propertys}") |
| 88 | + |
| 89 | +class Motorcycle(Vehicle): |
| 90 | + def accelerate(self) -> str: |
| 91 | + propertys: str = f"{self.brand} - {self.model}" |
| 92 | + return (f"Acelerando Motocicleta: {propertys}") |
| 93 | + |
| 94 | + def brake(self) -> str: |
| 95 | + propertys: str = f"{self.brand} - {self.model}" |
| 96 | + return (f"Frenando Motocicleta: {propertys}") |
| 97 | +class Truck(Vehicle): |
| 98 | + def accelerate(self) -> str: |
| 99 | + propertys: str = f"{self.brand} - {self.model}" |
| 100 | + return (f"Acelerando Camión: {propertys}") |
| 101 | + |
| 102 | + def brake(self) -> str: |
| 103 | + propertys: str = f"{self.brand} - {self.model}" |
| 104 | + return (f"Frenando Camión: {propertys}") |
| 105 | + |
| 106 | +# _________________ |
| 107 | +# Verificar cumplimiento de LSP |
| 108 | +def test_sub_class(sub_class: Vehicle): |
| 109 | + print("\nPropiedades:") |
| 110 | + print(f"{sub_class.brand} - {sub_class.model}") |
| 111 | + |
| 112 | + print("\nMetodos:") |
| 113 | + print(sub_class.accelerate()) |
| 114 | + print(sub_class.brake()) |
| 115 | + |
| 116 | +# _________________ |
| 117 | + |
| 118 | +car = Car("Honda", "Civic") |
| 119 | +test_sub_class(car) |
| 120 | + |
| 121 | +motorcycle = Motorcycle("Kawasaki", "Ninja") |
| 122 | +test_sub_class(motorcycle) |
| 123 | + |
| 124 | +truck = Truck("Ford", "Raptor") |
| 125 | +test_sub_class(truck) |
| 126 | + |
| 127 | +# end |
0 commit comments