Skip to content

Commit 68a4523

Browse files
committed
Composite Pattern
1 parent 66e6534 commit 68a4523

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Example/Composite-Pattern.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
// 组合模式(Composite Pattern)
1+
// 组合模式(Composite Pattern)
2+
3+
// 指令
4+
let directive = {
5+
eat: {
6+
excute: () => {
7+
console.log('eat')
8+
}
9+
},
10+
sleep: {
11+
excute: () => {
12+
console.log('sleep')
13+
}
14+
},
15+
code: {
16+
excute: () => {
17+
console.log('Get out there and write code!')
18+
}
19+
}
20+
}
21+
22+
const callDirective = () => {
23+
let result = {
24+
directiveList: [],
25+
add: (directive) => {
26+
result.directiveList.push(directive)
27+
},
28+
excute: () => {
29+
for (let ele of result.directiveList.values()) {
30+
ele.excute()
31+
}
32+
}
33+
}
34+
return result
35+
}
36+
37+
let command = callDirective()
38+
command.add(directive.eat)
39+
command.add(directive.sleep)
40+
command.add(directive.code)
41+
command.excute()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
5757
定义:又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。
5858
目的:将对象组合成树形结构以表示"部分-整体"的层次结构。
59-
场景:您想表示对象的部分-整体层次结构树形结构
59+
场景:您想表示对象的部分-整体层次结构(树形结构),如:文件系统
6060

6161
> [装饰模式(Decorator Pattern)](/Example/Decorator-Pattern.js)
6262

0 commit comments

Comments
 (0)