Skip to content

Commit 2767286

Browse files
authored
Merge pull request mouredev#4864 from hozlucas28/Solution-28-TypeScript
#28 - TypeScript
2 parents ae6fb16 + 989fc76 commit 2767286

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
Liskov Substitution Principle (LCP)...
3+
*/
4+
5+
console.log('Liskov Substitution Principle (LCP)...')
6+
7+
console.log('\nBad implementation of Liskov Substitution Principle (LCP)...')
8+
9+
interface IBadAnimal {
10+
walk(): void
11+
}
12+
13+
abstract class BadAnimal implements IBadAnimal {
14+
public abstract walk(): void
15+
}
16+
17+
function walk(animal: BadAnimal) {
18+
animal.walk()
19+
}
20+
21+
class BadDog extends BadAnimal {
22+
public walk(): void {
23+
console.log('Dog walking!')
24+
}
25+
}
26+
27+
class BadFish extends BadAnimal {
28+
public walk(): void {
29+
throw new Error('Fish can not fly')
30+
}
31+
}
32+
33+
console.log(`\n\`\`\`\ninterface IBadAnimal {
34+
walk(): void
35+
}
36+
37+
abstract class BadAnimal implements IBadAnimal {
38+
public abstract walk(): void
39+
}
40+
41+
function walk(animal: BadAnimal) {
42+
animal.walk()
43+
}
44+
45+
class BadDog extends BadAnimal {
46+
public walk(): void {
47+
console.log('Dog walking!')
48+
}
49+
}
50+
51+
class BadFish extends BadAnimal {
52+
public walk(): void {
53+
throw new Error('Fish can not fly')
54+
}
55+
}\n\`\`\``)
56+
57+
console.log(
58+
'\nThis is a bad implementation of Liskov Substitution Principle (LCP),\n' +
59+
'because the "BadFish" class implements the "walk" method which produces a\n' +
60+
'different side effect than the "walk" method inside the "BadDog" class. So,\n' +
61+
'if we execute the "walk" function with both classes ("BadDog", and "BadFish") the\n' +
62+
'function will produce different side effects that could be break the function execution.'
63+
)
64+
65+
console.log('\nGood implementation of Liskov Substitution Principle (LCP)...')
66+
67+
interface IGoodAnimal {
68+
move(): void
69+
}
70+
71+
abstract class GoodAnimal implements IGoodAnimal {
72+
public abstract move(): void
73+
}
74+
75+
function move(animal: GoodAnimal): void {
76+
animal.move()
77+
}
78+
79+
class GoodDog extends GoodAnimal {
80+
public move(): void {
81+
console.log('Dog walking!')
82+
}
83+
}
84+
85+
class GoodFish extends GoodAnimal {
86+
public move(): void {
87+
console.log('Fish swimming!')
88+
}
89+
}
90+
91+
console.log(`\n\`\`\`\ninterface IGoodAnimal {
92+
move(): void
93+
}
94+
95+
abstract class GoodAnimal implements IGoodAnimal {
96+
public abstract move(): void
97+
}
98+
99+
function move(animal: GoodAnimal): void {
100+
animal.move()
101+
}
102+
103+
class GoodDog extends GoodAnimal {
104+
public move(): void {
105+
console.log('Dog walking!')
106+
}
107+
}
108+
109+
class GoodFish extends GoodAnimal {
110+
public move(): void {
111+
console.log('Fish swimming!')
112+
}
113+
}\n\`\`\``)
114+
115+
console.log(
116+
'\nThis is a good implementation of Liskov Substitution Principle (LCP),\n' +
117+
'because all child classes of "GoodAnimal" class implements each method\n' +
118+
'with the same side effect. So, if we execute the "move" function with both\n' +
119+
'classes ("GoodDog", and "GoodFish"), we are not going to have side effects that\n' +
120+
'could break the function execution.'
121+
)
122+
123+
console.log(
124+
'\n# ---------------------------------------------------------------------------------- #\n'
125+
)
126+
127+
/*
128+
Additional challenge...
129+
*/
130+
131+
console.log('Additional challenge...')
132+
133+
interface IVehicle {
134+
brake(): void
135+
speedUp(): void
136+
}
137+
138+
abstract class Vehicle implements IVehicle {
139+
public abstract brake(): void
140+
public abstract speedUp(): void
141+
}
142+
143+
function brake(vehicle: Vehicle) {
144+
vehicle.brake()
145+
}
146+
147+
function speedUp(vehicle: Vehicle) {
148+
vehicle.speedUp()
149+
}
150+
151+
class Car extends Vehicle {
152+
public brake(): void {
153+
console.log('Car braking!')
154+
}
155+
156+
public speedUp(): void {
157+
console.log('Car speeding up!')
158+
}
159+
}
160+
161+
class Truck extends Vehicle {
162+
public brake(): void {
163+
console.log('Truck braking!')
164+
}
165+
166+
public speedUp(): void {
167+
console.log('Truck speeding up!')
168+
}
169+
}
170+
171+
class Boat extends Vehicle {
172+
public brake(): void {
173+
console.log('Boat braking!')
174+
}
175+
176+
public speedUp(): void {
177+
console.log('Boat speeding up!')
178+
}
179+
}
180+
181+
const car: Car = new Car()
182+
const truck: Truck = new Truck()
183+
const boat: Boat = new Boat()
184+
185+
console.log()
186+
speedUp(car)
187+
speedUp(truck)
188+
speedUp(boat)
189+
190+
console.log()
191+
brake(car)
192+
brake(truck)
193+
brake(boat)

0 commit comments

Comments
 (0)