-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
124 lines (105 loc) · 2.71 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
interface FlyBehavior {
fly: () => void;
}
interface QuackBehavior {
quack: () => void;
}
abstract class Duck {
abstract flyBehavior: FlyBehavior;
abstract quackBehavior: QuackBehavior;
abstract display: () => void;
public performFly() {
this.flyBehavior.fly();
}
public performQuack() {
this.quackBehavior.quack();
}
public swim() {
console.log('All ducks float, even decoys!');
}
public setFlyBehavior(fb: FlyBehavior) {
this.flyBehavior = fb;
}
public setQuackBehavior(qb: QuackBehavior) {
this.quackBehavior = qb;
}
}
// Declaring the same interface with a few different syntax approaches
// intentionally here. As the book continues, I will start to enforce more
// and more settings in the compiler.
class FlyWithWings implements FlyBehavior {
public fly(): void {
console.log("I'm flying!");
}
}
class FlyNoWay implements FlyBehavior {
fly() {
console.log("I can't fly");
}
}
class Quack implements QuackBehavior {
public quack() {
console.log('Quack');
}
}
class MuteQuack implements QuackBehavior {
public quack() {
console.log('<< Silence >>');
}
}
class Squeak implements QuackBehavior {
public quack() {
console.log('Squeak');
}
}
class MallardDuck extends Duck {
flyBehavior = new FlyWithWings();
quackBehavior = new Quack();
display = () => console.log("I'm a real Mallard Duck");
}
class RubberDuck extends Duck {
flyBehavior = new FlyNoWay();
quackBehavior = new MuteQuack();
display = () => console.log('I am a Rubber Duck!');
}
class ModelDuck extends Duck {
flyBehavior = new FlyNoWay();
quackBehavior = new Quack();
display = () => console.log("I'm a model duck");
}
class FlyRocketPowered implements FlyBehavior {
fly() {
console.log("I'm flying with a rocket!");
}
}
const exampleOne = () => {
console.log('Strategy Pattern: Example 1\n');
console.log('Incoming Mallard Duck!');
const mallard = new MallardDuck();
mallard.performQuack();
mallard.performFly();
console.log('Now the rubber duck!');
const rubberDuck = new RubberDuck();
rubberDuck.performQuack();
rubberDuck.performFly();
console.log('\n**DONE**');
};
const exampleTwo = () => {
console.log('Strategy Pattern: Example 2\n');
console.log('Our implementation allows for dynamic assignment of behaviors.');
const model = new ModelDuck();
console.log('Executing and changing Fly...');
model.performFly();
model.setFlyBehavior(new FlyRocketPowered());
model.performFly();
console.log('Executing and changing Quack...');
model.performQuack();
model.setQuackBehavior(new Squeak());
model.performQuack();
console.log('\n**DONE**');
};
const ChapterOne = {
exampleOne,
exampleTwo,
};
export default ChapterOne;