|
| 1 | +/* |
| 2 | +╔═══════════════════════════════════════╗ |
| 3 | +║ Autor: Kenys Alvarado ║ |
| 4 | +║ GitHub: https://github.com/Kenysdev ║ |
| 5 | +║ 2024 - Rust ║ |
| 6 | +╚═══════════════════════════════════════╝ |
| 7 | +------------------------------------------------- |
| 8 | +* SOLID: PRINCIPIO DE SUSTITUCIÓN DE LISKOV (LSP) |
| 9 | +------------------------------------------------- |
| 10 | +*/ |
| 11 | + |
| 12 | +trait BaseT { |
| 13 | + fn method_base(&self); |
| 14 | +} |
| 15 | + |
| 16 | +// Implementación |
| 17 | +struct Sub1; |
| 18 | + |
| 19 | +impl BaseT for Sub1 { |
| 20 | + fn method_base(&self) { |
| 21 | + println!("Sub1"); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Implementación |
| 26 | +struct Sub2; |
| 27 | + |
| 28 | +impl BaseT for Sub2 { |
| 29 | + fn method_base(&self) { |
| 30 | + println!("Sub2"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/* |
| 35 | +_______________ |
| 36 | +* EJERCICIO: |
| 37 | +* Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como |
| 38 | +* cumplir el LSP. |
| 39 | +* Instrucciones: |
| 40 | +* 1. Crea la clase Vehículo. |
| 41 | +* 2. Añade tres subclases de Vehículo. |
| 42 | +* 3. Implementa las operaciones "acelerar" y "frenar" como corresponda. |
| 43 | +* 4. Desarrolla un código que compruebe que se cumple el LSP. |
| 44 | +*/ |
| 45 | + |
| 46 | +// Trait base |
| 47 | +trait Vehicle { |
| 48 | + fn brand(&self) -> &str; |
| 49 | + fn model(&self) -> &str; |
| 50 | + fn accelerate(&self) -> String; |
| 51 | + fn brake(&self) -> String; |
| 52 | +} |
| 53 | + |
| 54 | +// ___________________________________________ |
| 55 | +// Implementación |
| 56 | +struct Car { |
| 57 | + brand: String, |
| 58 | + model: String, |
| 59 | +} |
| 60 | + |
| 61 | +impl Car { |
| 62 | + fn new(brand: &str, model: &str) -> Car { |
| 63 | + Car { |
| 64 | + brand: brand.to_string(), |
| 65 | + model: model.to_string(), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Vehicle for Car { |
| 71 | + fn brand(&self) -> &str { |
| 72 | + &self.brand |
| 73 | + } |
| 74 | + |
| 75 | + fn model(&self) -> &str { |
| 76 | + &self.model |
| 77 | + } |
| 78 | + |
| 79 | + fn accelerate(&self) -> String { |
| 80 | + format!("Acelerando auto: {} - {}", self.brand, self.model) |
| 81 | + } |
| 82 | + |
| 83 | + fn brake(&self) -> String { |
| 84 | + format!("Frenando auto: {} - {}", self.brand, self.model) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// ___________________________________________ |
| 89 | +// Implementación |
| 90 | +struct Motorcycle { |
| 91 | + brand: String, |
| 92 | + model: String, |
| 93 | +} |
| 94 | + |
| 95 | +impl Motorcycle { |
| 96 | + fn new(brand: &str, model: &str) -> Motorcycle { |
| 97 | + Motorcycle { |
| 98 | + brand: brand.to_string(), |
| 99 | + model: model.to_string(), |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Vehicle for Motorcycle { |
| 105 | + fn brand(&self) -> &str { |
| 106 | + &self.brand |
| 107 | + } |
| 108 | + |
| 109 | + fn model(&self) -> &str { |
| 110 | + &self.model |
| 111 | + } |
| 112 | + |
| 113 | + fn accelerate(&self) -> String { |
| 114 | + format!("Acelerando Motocicleta: {} - {}", self.brand, self.model) |
| 115 | + } |
| 116 | + |
| 117 | + fn brake(&self) -> String { |
| 118 | + format!("Frenando Motocicleta: {} - {}", self.brand, self.model) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// ___________________________________________ |
| 123 | +// Implementación |
| 124 | +struct Truck { |
| 125 | + brand: String, |
| 126 | + model: String, |
| 127 | +} |
| 128 | + |
| 129 | +impl Truck { |
| 130 | + fn new(brand: &str, model: &str) -> Truck { |
| 131 | + Truck { |
| 132 | + brand: brand.to_string(), |
| 133 | + model: model.to_string(), |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl Vehicle for Truck { |
| 139 | + fn brand(&self) -> &str { |
| 140 | + &self.brand |
| 141 | + } |
| 142 | + |
| 143 | + fn model(&self) -> &str { |
| 144 | + &self.model |
| 145 | + } |
| 146 | + |
| 147 | + fn accelerate(&self) -> String { |
| 148 | + format!("Acelerando Camión: {} - {}", self.brand, self.model) |
| 149 | + } |
| 150 | + |
| 151 | + fn brake(&self) -> String { |
| 152 | + format!("Frenando Camión: {} - {}", self.brand, self.model) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// ___________________________________________ |
| 157 | +fn test_impls(vehicle: &dyn Vehicle) { |
| 158 | + println!("\nPropiedades:"); |
| 159 | + println!("{} - {}", vehicle.brand(), vehicle.model()); |
| 160 | + |
| 161 | + println!("\nMétodos:"); |
| 162 | + println!("{}", vehicle.accelerate()); |
| 163 | + println!("{}", vehicle.brake()); |
| 164 | +} |
| 165 | + |
| 166 | +fn main() { |
| 167 | + let s1 = Sub1; |
| 168 | + let s2 = Sub2; |
| 169 | + |
| 170 | + s1.method_base(); |
| 171 | + s2.method_base(); |
| 172 | + |
| 173 | + // _______________________________________________ |
| 174 | + // EXS |
| 175 | + let car = Car::new("Honda", "Civic"); |
| 176 | + test_impls(&car); |
| 177 | + |
| 178 | + let motorcycle = Motorcycle::new("Kawasaki", "Ninja"); |
| 179 | + test_impls(&motorcycle); |
| 180 | + |
| 181 | + let truck = Truck::new("Ford", "Raptor"); |
| 182 | + test_impls(&truck); |
| 183 | + |
| 184 | +} |
0 commit comments