|
| 1 | +# 28 SOLID: PRINCIPIO DE SUSTITUCIÓN DE LISKOV (LSP) |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +# Ejercicio |
| 4 | + |
| 5 | + |
| 6 | +class Pato: |
| 7 | + def __init__(self, tipo): |
| 8 | + self.tipo = tipo |
| 9 | + |
| 10 | + |
| 11 | +class Pato_Acciones(Pato): |
| 12 | + def vuela(self): |
| 13 | + if self.tipo in ["domestico", "salvaje"]: |
| 14 | + print(f"El pato {self.tipo} vuela.") |
| 15 | + else: |
| 16 | + print(f"El pato {self.tipo} no puede volar.") |
| 17 | + |
| 18 | + def nada(self): |
| 19 | + if self.tipo in ["domestico", "salvaje"]: |
| 20 | + print(f"El pato {self.tipo} nada.") |
| 21 | + else: |
| 22 | + print(f"El pato {self.tipo} no puede nadar.") |
| 23 | + |
| 24 | + def dice(self): |
| 25 | + if self.tipo in ["domestico", "salvaje", "plastico"]: |
| 26 | + print(f"El pato {self.tipo} dice Quack.") |
| 27 | + else: |
| 28 | + print(f"El pato {self.tipo} no puede hacer Quack.") |
| 29 | + |
| 30 | + |
| 31 | +# Uso |
| 32 | +print("Sin LSP") |
| 33 | +pato1 = Pato_Acciones(tipo="salvaje") |
| 34 | +pato2 = Pato_Acciones(tipo="domestico") |
| 35 | +pato3 = Pato_Acciones(tipo="plastico") |
| 36 | + |
| 37 | +pato1.vuela() |
| 38 | +pato1.nada() |
| 39 | +pato1.dice() |
| 40 | + |
| 41 | +pato2.vuela() |
| 42 | +pato2.nada() |
| 43 | +pato2.dice() |
| 44 | + |
| 45 | +pato3.vuela() |
| 46 | +pato3.nada() |
| 47 | +pato3.dice() |
| 48 | + |
| 49 | +# Ejemplo Pato con LSP |
| 50 | + |
| 51 | + |
| 52 | +class Pato (ABC): |
| 53 | + def __init__(self, tipo): |
| 54 | + self.tipo = tipo |
| 55 | + |
| 56 | + @abstractmethod |
| 57 | + def vuela(self): |
| 58 | + pass |
| 59 | + |
| 60 | + @abstractmethod |
| 61 | + def nada(self): |
| 62 | + pass |
| 63 | + |
| 64 | + @abstractmethod |
| 65 | + def dice(self): |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +class PatoSalvaje(Pato): |
| 70 | + def __init__(self): |
| 71 | + super().__init__("salvaje") |
| 72 | + |
| 73 | + def vuela(self): |
| 74 | + print("El pato salvaje vuela.") |
| 75 | + |
| 76 | + def nada(self): |
| 77 | + print("El pato salvaje nada.") |
| 78 | + |
| 79 | + def dice(self): |
| 80 | + print("El pato salvaje dice Quack.") |
| 81 | + |
| 82 | + |
| 83 | +class PatoDomestico(Pato): |
| 84 | + def __init__(self): |
| 85 | + super().__init__("domestico") |
| 86 | + |
| 87 | + def vuela(self): |
| 88 | + print("El pato doméstico vuela.") |
| 89 | + |
| 90 | + def nada(self): |
| 91 | + print("El pato doméstico nada.") |
| 92 | + |
| 93 | + def dice(self): |
| 94 | + print("El pato doméstico dice Quack.") |
| 95 | + |
| 96 | + |
| 97 | +class PatoPlastico(Pato): |
| 98 | + def __init__(self): |
| 99 | + super().__init__("plastico") |
| 100 | + |
| 101 | + def vuela(self): |
| 102 | + print("El pato de plástico no puede volar.") |
| 103 | + |
| 104 | + def nada(self): |
| 105 | + print("El pato de plástico no puede nadar.") |
| 106 | + |
| 107 | + def dice(self): |
| 108 | + print("El pato de plástico dice Squeak.") |
| 109 | + |
| 110 | + |
| 111 | +# Uso |
| 112 | +print("Con LSP") |
| 113 | +pato1 = PatoSalvaje() |
| 114 | +pato2 = PatoDomestico() |
| 115 | +pato3 = PatoPlastico() |
| 116 | + |
| 117 | +pato1.vuela() |
| 118 | +pato1.nada() |
| 119 | +pato1.dice() |
| 120 | + |
| 121 | +pato2.vuela() |
| 122 | +pato2.nada() |
| 123 | +pato2.dice() |
| 124 | + |
| 125 | +pato3.vuela() |
| 126 | +pato3.nada() |
| 127 | +pato3.dice() |
| 128 | + |
| 129 | +# Extra |
| 130 | + |
| 131 | + |
| 132 | +class Vehiculo (ABC): |
| 133 | + def __init__(self, tipo): |
| 134 | + self.tipo = tipo |
| 135 | + |
| 136 | + @abstractmethod |
| 137 | + def acelera(self, velocidad, tiempo): |
| 138 | + pass |
| 139 | + |
| 140 | + @abstractmethod |
| 141 | + def frena(self, velocidad, tiempo): |
| 142 | + pass |
| 143 | + |
| 144 | + |
| 145 | +class Moto(Vehiculo): |
| 146 | + def __init__(self): |
| 147 | + super().__init__("moto") |
| 148 | + |
| 149 | + def acelera(self, velocidad_inicial, tiempo): |
| 150 | + aceleracion = 25 # km/h/s |
| 151 | + velocidad_final = velocidad_inicial + aceleracion * tiempo |
| 152 | + return min(velocidad_final, 220) |
| 153 | + |
| 154 | + def frena(self, velocidad_inicial, tiempo): |
| 155 | + desaceleracion = 30 # km/h/s |
| 156 | + velocidad_final = velocidad_inicial - desaceleracion * tiempo |
| 157 | + return max(velocidad_final, 0) |
| 158 | + |
| 159 | + |
| 160 | +class Auto(Vehiculo): |
| 161 | + def __init__(self): |
| 162 | + super().__init__("auto") |
| 163 | + |
| 164 | + def acelera(self, velocidad_inicial, tiempo): |
| 165 | + aceleracion = 15 # km/h/s |
| 166 | + velocidad_final = velocidad_inicial + aceleracion * tiempo |
| 167 | + return min(velocidad_final, 240) |
| 168 | + |
| 169 | + def frena(self, velocidad_inicial, tiempo): |
| 170 | + desaceleracion = 25 # km/h/s |
| 171 | + velocidad_final = velocidad_inicial - desaceleracion * tiempo |
| 172 | + return max(velocidad_final, 0) |
| 173 | + |
| 174 | + |
| 175 | +class Camion(Vehiculo): |
| 176 | + def __init__(self): |
| 177 | + super().__init__("camion") |
| 178 | + |
| 179 | + def acelera(self, velocidad_inicial, tiempo): |
| 180 | + aceleracion = 4 # km/h/s |
| 181 | + velocidad_final = velocidad_inicial + aceleracion * tiempo |
| 182 | + return min(velocidad_final, 110) |
| 183 | + |
| 184 | + def frena(self, velocidad_inicial, tiempo): |
| 185 | + desaceleracion = 12 # km/h/s |
| 186 | + velocidad_final = velocidad_inicial - desaceleracion * tiempo |
| 187 | + return max(velocidad_final, 0) |
| 188 | + |
| 189 | + |
| 190 | +# Uso |
| 191 | +print("Con LSP") |
| 192 | +moto = Moto() |
| 193 | +auto = Auto() |
| 194 | +camion = Camion() |
| 195 | + |
| 196 | +# Ejemplo para acelerar y frenar |
| 197 | +velocidad_inicial = 25 |
| 198 | +tiempo_acelerar = 4 |
| 199 | +tiempo_frenar = 2 |
| 200 | + |
| 201 | +velocidad_acelerada = moto.acelera(velocidad_inicial, tiempo_acelerar) |
| 202 | +velocidad_frenada = moto.frena(velocidad_acelerada, tiempo_frenar) |
| 203 | +print(f"La velocidad de la moto después de acelerar durante { |
| 204 | + tiempo_acelerar} segundos es de {velocidad_acelerada} km/h") |
| 205 | +print(f"La velocidad de la moto después de frenar durante { |
| 206 | + tiempo_frenar} segundos es de {velocidad_frenada} km/h") |
| 207 | + |
| 208 | +velocidad_acelerada = auto.acelera(velocidad_inicial, tiempo_acelerar) |
| 209 | +velocidad_frenada = auto.frena(velocidad_acelerada, tiempo_frenar) |
| 210 | +print(f"La velocidad del auto después de acelerar durante { |
| 211 | + tiempo_acelerar} segundos es de {velocidad_acelerada} km/h") |
| 212 | +print(f"La velocidad del auto después de frenar durante { |
| 213 | + tiempo_frenar} segundos es de {velocidad_frenada} km/h") |
| 214 | + |
| 215 | +velocidad_acelerada = camion.acelera(velocidad_inicial, tiempo_acelerar) |
| 216 | +velocidad_frenada = camion.frena(velocidad_acelerada, tiempo_frenar) |
| 217 | +print(f"La velocidad del camion después de acelerar durante { |
| 218 | + tiempo_acelerar} segundos es de {velocidad_acelerada} km/h") |
| 219 | +print(f"La velocidad del camion después de frenar durante { |
| 220 | + tiempo_frenar} segundos es de {velocidad_frenada} km/h") |
0 commit comments