-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample03.ts
More file actions
61 lines (49 loc) · 1.17 KB
/
example03.ts
File metadata and controls
61 lines (49 loc) · 1.17 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
class Light {
private isOn = false;
turnOn(): void {
this.isOn = true;
console.log("💡 Світло увімкнено");
}
turnOff(): void {
this.isOn = false;
console.log("🔌 Світло вимкнено");
}
}
interface Command {
execute(): void;
undo(): void;
}
class TurnOnCommand implements Command {
constructor(private light: Light) { }
execute(): void {
this.light.turnOn();
}
undo(): void {
this.light.turnOff();
}
}
class RemoteControl {
private command: Command | null = null;
private history: Command[] = [];
setCommand(command: Command): void {
this.command = command;
}
pressButton(): void {
if (this.command) {
this.command.execute();
this.history.push(this.command);
}
}
pressUndo(): void {
const lastCommand = this.history.pop();
if (lastCommand) {
lastCommand.undo();
}
}
}
const light = new Light();
const command = new TurnOnCommand(light);
const remote = new RemoteControl();
remote.setCommand(command);
remote.pressButton();
remote.pressUndo();