Skip to content

Commit 85b820c

Browse files
authored
Merge pull request mouredev#4950 from JesusAntonioEEscamilla/JesusAEE
#28 - JavaScript "Corrección"
2 parents 2a7f88c + 9a59910 commit 85b820c

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

Roadmap/28 - SOLID LSP/javascript/JesusAntonioEEscamilla.js

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,45 @@
1111
//INCORRECTO
1212
class Bird_{
1313
fly(){
14-
console.log("Flying");
14+
throw new Error("This method should be overridden");
1515
}
1616
}
1717

1818
class Eagle_ extends Bird_{
1919
fly(){
20-
console.log("Eagle flying high!");
20+
return "Eagle flying high!";
2121
}
2222
}
2323

2424
class Penguin_ extends Bird_{
2525
fly(){
26-
throw new Error("Penguins can't fly!!");
26+
return"Penguin can't flying!";
2727
}
2828
}
2929

30-
function makeBirdFly_(bird) {
31-
bird.fly();
32-
}
33-
3430
const eagle_ = new Eagle_();
3531
const penguin_ = new Penguin_();
3632

37-
makeBirdFly_(eagle_);
38-
// makeBirdFly_(penguin_);
33+
console.log(eagle_.fly());
34+
console.log(penguin_.fly());
3935

4036

4137
//CORRECTO
4238
class Bird{
4339
move(){
44-
console.log("Moving");
45-
}
46-
}
47-
48-
class FlyingBird extends Bird{
49-
fly(){
50-
console.log("Flying");
40+
throw new Error("This method should be overridden");
5141
}
5242
}
5343

54-
class Eagle extends FlyingBird{
55-
fly(){
56-
console.log("Eagle flying high!!");
44+
class Eagle extends Bird{
45+
move(){
46+
console.log("Eagle is Walking!!");
5747
}
5848
}
5949

6050
class Penguin extends Bird{
6151
move(){
62-
console.log("Penguin waddling");
52+
console.log("Penguin is Walking!!");
6353
}
6454
}
6555

@@ -79,64 +69,72 @@ makeBirdMove(penguin);
7969

8070
// Creando la clase
8171
class Vehículos{
72+
constructor(){
73+
this.speed = 0;
74+
}
75+
8276
acelerar(){
8377
throw new Error("Método 'acelerar' debe ser implementado");
8478
}
8579

8680
frenar(){
8781
throw new Error("Método 'frenar' debe ser implementado");
8882
}
83+
84+
getSpeed(){
85+
return this.speed;
86+
}
8987
}
9088

9189
// Sub-clases la clase principal
9290
class Coche extends Vehículos{
93-
acelerar(){
94-
console.log("El coche esta acelerando");
91+
acelerar(speed){
92+
console.log(`El coche esta acelerando, con ${speed} km/h`);
9593
}
9694

97-
frenar(){
98-
console.log("El coche esta frenando");
95+
frenar(speed){
96+
console.log(`El coche esta frenando, con ${speed} km/h`);
9997
}
10098
}
10199

102100
class Trailer extends Vehículos{
103-
acelerar(){
104-
console.log("El trailer esta acelerando");
101+
acelerar(speed){
102+
console.log(`El tailer esta acelerando, con ${speed} km/h`);
105103
}
106-
107-
frenar(){
108-
console.log("El trailer esta frenando");
104+
105+
frenar(speed){
106+
console.log(`El trailer esta frenando, con ${speed} km/h`);
109107
}
110108
}
111109

112110
class Moto extends Vehículos{
113-
acelerar(){
114-
console.log("La moto esta acelerando");
111+
acelerar(speed){
112+
console.log(`El moto esta acelerando, con ${speed} km/h`);
115113
}
116114

117-
frenar(){
118-
console.log("La moto esta frenando");
115+
frenar(speed){
116+
console.log(`El moto esta frenando, con ${speed} km/h`);
119117
}
120118
}
121119

122120
// Comprobación del LSP
123121
function pruebaLSP(vehículo) {
124-
vehículo.acelerar();
125-
vehículo.frenar();
122+
vehículo.acelerar(2);
123+
vehículo.frenar(1);
126124
}
127125

128126
// Ejemplo LSP
129127
const coche = new Coche();
130128
const trailer = new Trailer();
131129
const moto = new Moto();
132130

133-
console.log("Prueba con Coche:");
131+
// console.log("Prueba con Coche:");
134132
pruebaLSP(coche);
135133

136-
console.log("Prueba con Trailer:");
134+
// console.log("Prueba con Trailer:");
137135
pruebaLSP(trailer);
138136

139-
console.log("Prueba con Moto:");
137+
// console.log("Prueba con Moto:");
140138
pruebaLSP(moto);
141139

142140
/**-----DIFICULTAD EXTRA-----*/

0 commit comments

Comments
 (0)