结构型-组合模式
# 什么是组合模式
- Composite Design Pattern
- 主要处理树形结构数据,可以理解为一组对象集合。
- 将一组对象组织成树形结构,以表示“部分”、“整体”的层次结构。
- 组合让客户端可以统一单一对象和组合对象的处理逻辑。
# 组合模式的应用场景
- 部门和员工关系
- Linux 系统中的文件系统
# 组合模式条件
- 组合模式,将一组对象组织成树形结构,将单个对象和组合对象都看作树中的节点,以统一处理逻辑,并且它利用树形结构的特点,递归处理每个子树,一次简化代码实现。
- 使用组合模式的前提是,你的业务场景必须能够表示成树形结构。
# 代码实现
public interface IEmployee {
public void add(IEmployee employee);
public void reemove(IEmployee employee);
public IEmployee getChild(int i);
public String getName();
public double getSalary();
public void print();
}
{
private String name;
private double salary;
private List<IEmployee> employees = new ArrayList<>();
public Manager(String name, double salary) {
this.name = name;
this.salary = salary;
}
@Override
public void add(IEmployee employee) {
employees.add(employee);
}
@Override
public void reemove(IEmployee employee) {
employees.remove(employee);
}
@Override
public IEmployee getChild(int i) {
return employees.get(i);
}
@Override
public String getName() {
return this.name;
}
@Override
public double getSalary() {
return this.salary;
}
@Override
public void print() {
System.out.println("--------------------");
System.out.println("name: " + name);
System.out.println("Salary: " + salary);
System.out.println("--------------------");
for (IEmployee ep :
employees) {
ep.print();
}
}
}
public class Developer implements IEmployee {
private String name;
private double salary;
public Developer(String name, double salary) {
this.name = name;
this.salary = salary;
}
@Override
public void add(IEmployee employee) {
String msg = "not supported by leaf node";
throw new UnsupportedOperationException(msg);
}
@Override
public void reemove(IEmployee employee) {
String msg = "not supported by leaf node";
throw new UnsupportedOperationException(msg);
}
@Override
public IEmployee getChild(int i) {
String msg = "not supported by leaf node";
throw new UnsupportedOperationException(msg);
}
@Override
public String getName() {
return this.name;
}
@Override
public double getSalary() {
return this.salary;
}
@Override
public void print() {
System.out.println("--------------------");
System.out.println("name: " + name);
System.out.println("Salary: " + salary);
System.out.println("--------------------");
}
}
public static void main(String[] args) {
System.out.println("Hello World!");
Developer developer1 = new Developer("001", 011);
Developer developer2 = new Developer("002", 012);
Developer developer3 = new Developer("003", 013);
Manager manager = new Manager("000", 1000);
manager.add(developer1);
manager.add(developer2);
manager.add(developer3);
manager.print();
}
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
上次更新: 2020/09/08, 15:09:00