-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
188 lines (152 loc) · 3.62 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* The Command Pattern
*
* The Command Pattern encapsulates a request as an object, thereby letting you
* parameterize other objects with different requests, queue or log requests,
* and support undoable operations.
*/
interface Command {
execute: () => void;
undo: () => void;
}
class Light {
name: string;
constructor(name: string) {
this.name = name;
}
on() {
console.log(`Turning on ${this.name}`);
}
off() {
console.log(`Turning off ${this.name}`);
}
}
class LightOnCommand implements Command {
light: Light;
constructor(light: Light) {
this.light = light;
}
execute() {
this.light.on();
}
undo() {
this.light.off();
}
}
class LightOffCommand implements Command {
light: Light;
constructor(light: Light) {
this.light = light;
}
execute() {
this.light.off();
}
undo() {
this.light.on();
}
}
class SimpleRemoteControl {
slot: Command;
/**
* Passing in the command to the constructor here so the compiler doesn't
* complain
*/
constructor(command: Command) {
this.slot = command;
}
setCommand(command: Command) {
this.slot = command;
}
buttonWasPressed() {
this.slot.execute();
}
}
function exampleOne() {
const light = new Light('');
const lightOnCommand = new LightOnCommand(light);
const simpleRempte = new SimpleRemoteControl(lightOnCommand);
simpleRempte.buttonWasPressed();
}
class NoCommand implements Command {
execute() {
console.log('No Command!');
}
undo() {
console.log('I undo NOTHING!');
}
}
class Stereo {
on() {
console.log('Stereo on!');
}
off() {}
setCd() {
console.log('Set CD!');
}
removeCd() {}
setVolume(volume: number) {
console.log(`Volume: ${volume}`);
}
}
class StereoOnWIthCDCommand implements Command {
stereo: Stereo;
constructor(stereo: Stereo) {
this.stereo = stereo;
}
execute() {
this.stereo.on();
this.stereo.setCd();
this.stereo.setVolume(11);
}
undo() {
this.stereo.off();
this.stereo.removeCd();
this.stereo.setVolume(0);
}
}
class RemoteControl {
onCommands: Command[];
offCommands: Command[];
undoCommand: Command;
constructor() {
this.onCommands = Array(7).map(_e => new NoCommand());
this.offCommands = Array(7).map(_e => new NoCommand());
this.undoCommand = new NoCommand();
}
setCommand(slot: number, onCommand: Command, offCommand: Command) {
this.onCommands[slot] = onCommand;
this.offCommands[slot] = offCommand;
}
onButtonWasPushed(slot: number) {
this.onCommands[slot].execute();
this.undoCommand = this.onCommands[slot];
}
offButtonWasPushed(slot: number) {
this.offCommands[slot].execute();
this.undoCommand = this.offCommands[slot];
}
undoButtonWasPushed() {
this.undoCommand.undo();
}
}
function exampleTwo() {
const remoteControl = new RemoteControl();
const livingRoomLight = new Light('Living Room Light');
const livingRoomLightOn = new LightOnCommand(livingRoomLight);
const livingRoomLightOff = new LightOffCommand(livingRoomLight);
const kitchenLight = new Light('Kitchen Light');
const kitchenLightOn = new LightOnCommand(kitchenLight);
const kitchenLightOff = new LightOffCommand(kitchenLight);
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.onButtonWasPushed(0);
remoteControl.undoButtonWasPushed();
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(0);
remoteControl.offButtonWasPushed(1);
remoteControl.undoButtonWasPushed();
}
export default {
exampleOne,
exampleTwo,
};