组合模式

  • 将对象组合成树形结构以表示“部分-整体”的层次结构。组合使得用户对单个对象和组合对象的使用具有一致性。
  • 常见场景: 文件目录

代码实现

  • 组合对象和叶子对象并不是父子关系,而是组合关系
  • 实现时,我们会将组合对象类 Composite 或者叶子对象类 Leaf 继承于同一个抽象类 Component
1
2
3
4
5
6
7
8
9
10
11
12
13
abstract class Component {
operation() { }
}
class Composite extends Component {
operation() {
console.log('组合对象的实现');
}
}
class Leaf extends Component {
operation() {
console.log('叶子对象的实现');
}
}
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
abstract class TreeBase {
name: string;
nodes: TreeBase[] = [];
constructor(name: string) {
this.name = name;
}
add(n: TreeBase) {}
remove(n: TreeBase) {}
show() {}
}

class TreeComposite extends TreeBase {
constructor(name: string) {
super(name);
}

add(n: TreeBase) {
this.nodes.push(n);
}

remove(n: TreeBase) {
this.nodes.splice(this.nodes.findIndex((e) => e.name === n.name), 1);
}

show() {
console.log(`${this.name}, I am a composite node`);
this.nodes.forEach((e) => {
e.show();
});
}
}
class Leaf extends TreeBase {
constructor(name: string) {
super(name);
}

show() {
console.log(`${this.name}, I am a leaf`);
}
}
const root = new TreeComposite('root');
const node1 = new TreeComposite('node1');
const leaf1 = new Leaf('leaf1');
const leaf2 = new Leaf('leaf2');
const leaf3 = new Leaf('leaf3');

root.add(node1);
root.add(leaf3);
node1.add(leaf1);
node1.add(leaf2);
root.show();
/**
*
* root, I am a composite node
* tree.component.ts:51 node1, I am a composite node
* tree.component.ts:64 leaf1, I am a leaf
* tree.component.ts:64 leaf2, I am a leaf
* tree.component.ts:64 leaf3, I am a leaf
*
**/

参考链接