1+ #28 { Retos para Programadores } Principio SOLID de Sustitución de Liskov (Liskov Substitution Principle, LSP)
2+
3+ # Bibliography reference:
4+ # The Web Development Glossary (Jens Oliver Meiert) (Z-Library)
5+ # I use GPT as a reference and sometimes to correct or generate proper comments.
6+
7+ """
8+ * EJERCICIO:
9+ * Explora el "Principio SOLID de Sustitución de Liskov (Liskov Substitution Principle, LSP)"
10+ * y crea un ejemplo simple donde se muestre su funcionamiento
11+ * de forma correcta e incorrecta.
12+ *
13+ * DIFICULTAD EXTRA (opcional):
14+ * Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como
15+ * cumplir el LSP.
16+ * Instrucciones:
17+ * 1. Crea la clase Vehículo.
18+ * 2. Añade tres subclases de Vehículo.
19+ * 3. Implementa las operaciones "acelerar" y "frenar" como corresponda.
20+ * 4. Desarrolla un código que compruebe que se cumple el LSP.
21+
22+ """
23+ import time
24+
25+ # Shorthan for print
26+ log = print
27+
28+ # Simulate the loading of the application
29+ def main ():
30+ # Simulate a delay before showing an alert
31+ time .sleep (2 )
32+ log ('Retosparaprogramadores #28.' )
33+
34+ # Base class for shapes
35+ class Shape :
36+ def area (self ):
37+ raise NotImplementedError ("Method 'area' must be implemented" )
38+
39+ # Rectangle class inheriting from Shape
40+ class Rectangle (Shape ):
41+ def __init__ (self , width , height ):
42+ self .width = width
43+ self .height = height
44+
45+ def area (self ):
46+ return self .width * self .height
47+
48+ # Square class inheriting from Shape
49+ class Square (Shape ):
50+ def __init__ (self , side ):
51+ self .side = side
52+
53+ def area (self ):
54+ return self .side * self .side
55+
56+ # Function to calculate the area of a shape
57+ def calculate_area (shape ):
58+ return shape .area ()
59+
60+ # Create instances of Rectangle and Square
61+ rectangle = Rectangle (83 , 105 )
62+ square = Square (40 )
63+
64+ # Log the areas of the shapes
65+ log (calculate_area (rectangle )) # 8715
66+ log (calculate_area (square )) # 1600
67+
68+ # Extra Difficulty Exercise
69+
70+ # Car class definition
71+ class Car :
72+ def __init__ (self , brand , model , max_speed , acceleration , deceleration ):
73+ self .brand = brand
74+ self .model = model
75+ self .max_speed = max_speed # Maximum speed in km/h
76+ self .acceleration = acceleration # Acceleration in km/h per second
77+ self .deceleration = deceleration # Deceleration in km/h per second
78+ self .current_speed = 0 # Current speed in km/h
79+
80+ def accelerate (self , seconds ):
81+ new_speed = self .current_speed + (self .acceleration * seconds )
82+ self .current_speed = min (new_speed , self .max_speed )
83+ log (f"{ self .brand } { self .model } accelerated to { self .current_speed } km/h." )
84+
85+ def brake (self , seconds ):
86+ new_speed = self .current_speed - (self .deceleration * seconds )
87+ self .current_speed = max (new_speed , 0 )
88+ log (f"{ self .brand } { self .model } braked to { self .current_speed } km/h." )
89+
90+ # SportsCar class inheriting from Car
91+ class SportsCar (Car ):
92+ def __init__ (self , brand , model ):
93+ super ().__init__ (brand , model , 300 , 30 , 20 ) # Hardcoded values for sports cars
94+
95+ # FamilyCar class inheriting from Car
96+ class FamilyCar (Car ):
97+ def __init__ (self , brand , model ):
98+ super ().__init__ (brand , model , 200 , 15 , 10 ) # Hardcoded values for family cars
99+
100+ # Function to test the car's acceleration and braking
101+ def test_car (car ):
102+ car .accelerate (5 )
103+ car .brake (2 )
104+
105+ # List of sports cars
106+ sports_cars = [
107+ {"brand" : "Ferrari" , "model" : "488" , "max_speed" : 330 , "acceleration" : 30 , "deceleration" : 20 },
108+ {"brand" : "Porsche" , "model" : "911 Turbo S" , "max_speed" : 320 , "acceleration" : 28 , "deceleration" : 18 },
109+ {"brand" : "Lamborghini" , "model" : "Huracán" , "max_speed" : 325 , "acceleration" : 32 , "deceleration" : 22 }
110+ ]
111+
112+ # List of family cars
113+ family_cars = [
114+ {"brand" : "Toyota" , "model" : "Sienna" , "max_speed" : 180 , "acceleration" : 10 , "deceleration" : 8 },
115+ {"brand" : "Honda" , "model" : "Odyssey" , "max_speed" : 175 , "acceleration" : 9 , "deceleration" : 7 },
116+ {"brand" : "Chrysler" , "model" : "Pacifica" , "max_speed" : 180 , "acceleration" : 9 , "deceleration" : 7 }
117+ ]
118+
119+ # Create instances of SportsCar and FamilyCar
120+ sports_car = SportsCar (sports_cars [0 ]['brand' ], sports_cars [0 ]['model' ]) # Using the first sports car from the list
121+ family_car = FamilyCar (family_cars [1 ]['brand' ], family_cars [1 ]['model' ]) # Using the second family car from the list
122+
123+ # Test the cars
124+ test_car (sports_car ) # Example output: Ferrari 488 accelerated to 150 km/h.
125+ test_car (family_car ) # Example output: Honda Odyssey accelerated to 75 km/h.
126+
127+ # Call the main function to simulate the loading of the application
128+ if __name__ == "__main__" :
129+ main ()
130+
131+ # Output:
132+ """
133+ Ferrari 488 accelerated to 150 km/h.
134+ Ferrari 488 braked to 110 km/h.
135+ Honda Odyssey accelerated to 75 km/h.
136+ Honda Odyssey braked to 55 km/h.
137+ Retosparaprogramadores #28.
138+
139+ """
0 commit comments