Skip to content

Commit d7508b4

Browse files
committed
#28 - JavaScript
1 parent aa58d8d commit d7508b4

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* EJERCICIO:
3+
* Explora el "Principio SOLID de Sustitución de Liskov (Liskov Substitution Principle, LSP)"
4+
* y crea un ejemplo simple donde se muestre su funcionamiento
5+
* de forma correcta e incorrecta.
6+
*
7+
* DIFICULTAD EXTRA (opcional):
8+
* Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como
9+
* cumplir el LSP.
10+
* Instrucciones:
11+
* 1. Crea la clase Vehículo.
12+
* 2. Añade tres subclases de Vehículo.
13+
* 3. Implementa las operaciones "acelerar" y "frenar" como corresponda.
14+
* 4. Desarrolla un código que compruebe que se cumple el LSP.
15+
*/
16+
//EJERCICIO
17+
18+
/*
19+
20+
"Si S es un subtipo de T, entonces los objetos de tipo T en un programa de computadora pueden ser sustituidos por objetos de tipo S, sin alterar ninguna de las propiedades deseables de ese programa."
21+
22+
*/
23+
class GeometricShape {
24+
constructor() {
25+
this.area = this.getArea();
26+
}
27+
28+
getArea() {
29+
return 1;
30+
}
31+
}
32+
33+
class Rectangle extends GeometricShape {
34+
constructor(height, width) {
35+
super();
36+
this.height = height;
37+
this.width = width;
38+
this.area = this.getArea();
39+
}
40+
41+
getArea() {
42+
return this.height * this.width;
43+
}
44+
}
45+
46+
class Square extends GeometricShape {
47+
constructor(height) {
48+
super();
49+
this.height = height;
50+
this.area = this.getArea();
51+
}
52+
53+
getArea() {
54+
return this.height ** 2;
55+
}
56+
}
57+
58+
class Circle extends GeometricShape {
59+
constructor(radius) {
60+
super();
61+
this.radius = radius;
62+
this.area = this.getArea();
63+
}
64+
65+
getArea() {
66+
return this.radius * 2 * Math.PI;
67+
}
68+
}
69+
70+
function displayArea(shape) {
71+
console.log(shape.area);
72+
}
73+
74+
let square = new Square(12);
75+
76+
displayArea(square);
77+
78+
//EXTRA
79+
class Vehicle {
80+
constructor() {
81+
this.speed = 0;
82+
}
83+
84+
accelerate(amount) {
85+
this.speed += amount * 1.2;
86+
}
87+
88+
brake(amount) {
89+
this.speed -= amount * 0.8;
90+
91+
if (this.speed < 0) {
92+
this.speed = 0;
93+
}
94+
}
95+
}
96+
97+
class Motorcycle extends Vehicle {
98+
constructor() {
99+
super();
100+
}
101+
102+
accelerate(amount) {
103+
this.speed += amount * 1.5;
104+
}
105+
106+
brake(amount) {
107+
this.speed -= amount * 1.2;
108+
109+
if (this.speed < 0) {
110+
this.speed = 0;
111+
}
112+
}
113+
}
114+
115+
class Car extends Vehicle {
116+
constructor() {
117+
super();
118+
}
119+
120+
accelerate(amount) {
121+
this.speed += amount * 1.6;
122+
}
123+
124+
brake(amount) {
125+
this.speed -= amount * 1.3;
126+
127+
if (this.speed < 0) {
128+
this.speed = 0;
129+
}
130+
}
131+
}
132+
133+
class SciFiTurboCar extends Vehicle {
134+
constructor() {
135+
super();
136+
}
137+
138+
accelerate(amount) {
139+
this.speed += amount * 3;
140+
}
141+
142+
brake(amount) {
143+
this.speed -= amount * 2.5;
144+
145+
if (this.speed < 0) {
146+
this.speed = 0;
147+
}
148+
}
149+
}
150+
151+
function checkLSP(vehicle) {
152+
if (!vehicle.accelerate || !vehicle.brake) {
153+
throw new Error('El objeto no cumple con la interfaz de Vehículo');
154+
}
155+
156+
vehicle.accelerate(78);
157+
vehicle.brake(4);
158+
159+
console.log(vehicle);
160+
}
161+
162+
const vehicle = new Vehicle();
163+
const toyota = new Car();
164+
const jaguar = new Motorcycle();
165+
const hypercar3000 = new SciFiTurboCar();
166+
167+
checkLSP(vehicle);
168+
checkLSP(toyota);
169+
checkLSP(jaguar);
170+
checkLSP(hypercar3000);

0 commit comments

Comments
 (0)