-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand3.js
More file actions
55 lines (49 loc) · 1.26 KB
/
Copy pathcommand3.js
File metadata and controls
55 lines (49 loc) · 1.26 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
/*
* @Author: junchao
* @Date: 2021-01-06 15:50:01
* @LastEditTime: 2021-01-06 15:51:08
* @LastEditors: junchao
* @Description:
* @FilePath: /JavaScriptDesignPatterns/command/command3.js
* @可以输入预定的版权声明、个性签名、空行等
*/
// 宏命令对象
class MacroCommand {
constructor() {
this.commandList = []; // 缓存子命令对象
}
add(command) { // 向缓存中添加子命令
this.commandList.push(command);
}
execute() { // 对外命令执行接口
// 遍历自命令对象并执行其 execute 方法
for (const command of this.commandList) {
command.execute();
}
}
}
const openWechat = { // 命令对象
execute: () => {
console.log('打开微信');
}
};
const openChrome = { // 命令对象
execute: () => {
console.log('打开Chrome');
}
};
const openEmail = { // 命令对象
execute: () => {
console.log('打开Email');
}
}
const macroCommand = new MacroCommand();
macroCommand.add(openWechat); // 宏命令中添加子命令
macroCommand.add(openChrome); // 宏命令中添加子命令
macroCommand.add(openEmail); // 宏命令中添加子命令
macroCommand.execute(); // 执行宏命令
/* 输出:
打开微信
打开Chrome
打开Email
*/