-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-closed-principle.ts
59 lines (52 loc) · 1.6 KB
/
open-closed-principle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* **Open-Closed Principle**
*
* You've maybe heard some variant of how a module
*
* > "[...] should be open for extension and closed for modification."
*
* This principle is closely related to polymorphism, as we use
* an abstraction (the Toy, in this case) to fulfill our principle.
*
* In terms of the "open" and "closed" notions, this can be more easily
* understood if you think of "open for extension" as referring to
* the abstractions, rather than the concrete implementation. Thus,
* you build new classes (such as a `CuddlyBear`) that implements the
* Toy abstraction; i.e. they are "open for extension". This keeps the
* unique properties and implementation details of each Toy discrete;
* i.e. they are "closed for modification".
*
* @see https://en.wikipedia.org/wiki/Open–closed_principle
*/
function ocpDemo() {
interface Toy {
play(): void;
}
class DancingRobot implements Toy {
play(): void {
console.log('Robot: The clanking robot slam-dances wildly!');
this.adjustHydraulics();
}
adjustHydraulics() {
console.log('Robot: Adjusting hydraulics...');
}
}
class SingingDinosaur implements Toy {
play(): void {
this.snack();
console.log('Dinosaur: The giant dinosaur roars into a song!');
}
snack() {
console.log(
'Dinosaur: Eating some lesser creatures as a pre-performance snack...'
);
}
}
class ToyPlayer {
playWithToys(toys: Toy[]): void {
for (const toy of toys) toy.play();
}
}
new ToyPlayer().playWithToys([new DancingRobot(), new SingingDinosaur()]);
}
ocpDemo();