-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample07.ts
More file actions
107 lines (84 loc) · 2 KB
/
example07.ts
File metadata and controls
107 lines (84 loc) · 2 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
interface Command {
execute(): void;
undo(): void;
}
class Archer {
place(): void {
console.log("Placed Archer at A1");
}
remove(): void {
console.log("Removed Archer from A1");
}
}
class Mage {
place(): void {
console.log("Placed Mage at A2");
}
remove(): void {
console.log("Removed Mage from A2");
}
}
class Warrior {
place(): void {
console.log("Placed Warrior at A3");
}
remove(): void {
console.log("Removed Warrior from A3");
}
}
class PlaceArcherCommand implements Command {
constructor(private archer: Archer) { }
execute(): void {
this.archer.place();
}
undo(): void {
this.archer.remove();
}
}
class PlaceMageCommand implements Command {
constructor(private mage: Mage) { }
execute(): void {
this.mage.place();
}
undo(): void {
this.mage.remove();
}
}
class PlaceWarriorCommand implements Command {
constructor(private warrior: Warrior) { }
execute(): void {
this.warrior.place();
}
undo(): void {
this.warrior.remove();
}
}
class MacroCommand implements Command {
constructor(private commands: Command[]) { }
execute(): void {
for (const command of this.commands) {
command.execute();
}
}
undo(): void {
for (const command of [...this.commands].reverse()) {
command.undo();
}
}
}
const archer = new Archer();
const mage = new Mage();
const warrior = new Warrior();
const placeArcher = new PlaceArcherCommand(archer);
const placeMage = new PlaceMageCommand(mage);
const placeWarrior = new PlaceWarriorCommand(warrior);
const squadDeployment = new MacroCommand([
placeArcher,
placeMage,
placeWarrior,
]);
squadDeployment.execute();
console.log('--------------------------------');
console.log('Виконуємо якісь інші дії');
console.log('--------------------------------');
squadDeployment.undo();