-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.js
More file actions
94 lines (71 loc) · 1.6 KB
/
Copy pathCommand.js
File metadata and controls
94 lines (71 loc) · 1.6 KB
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
// Design a remote control system that supports ON/OFF operations for devices, along with undo/redo capabilities.
class Light {
turnOn() {
console.info("Lights Turn On")
}
turnOff() {
console.info("Lights Turn Off");
}
}
class Command {
execute() {
throw new Error("Implement Execute")
}
undo() {
throw new Error("Implement undo")
}
redo() {
throw new Error("Implement redo")
}
}
class TurnOnCommand extends Command {
constructor(light) {
super()
this.light = light;
}
execute() {
this.light.turnOn();
}
undo() {
this.light.turnOff();
}
redo() {
this.light.turnOn();
}
}
class TurnOffCommand extends Command {
constructor(light) {
super()
this.light = light;
}
execute() {
this.light.turnOff();
}
undo() {
this.light.turnOn();
}
redo() {
this.light.turnOff();
}
}
// Invoker
class RemoteController {
setCommand(command) {
this.command = command;
}
execute() {
this.command.execute();
this.last_command_execute = this.command;
}
undo() { if(this.last_command_execute) this.last_command_execute.undo() };
redo() { if(this.last_command_execute) this.last_command_execute.redo() };
}
const light = new Light();
const LightsOn = new TurnOnCommand(light);
const LightsOff = new TurnOffCommand(light);
const remote = new RemoteController();
remote.setCommand(LightsOn);
remote.execute()
remote.undo()
remote.redo()
// Add Logic for Fan On and set speed