Skip to content

Commit 716f2dd

Browse files
committed
设计模式-组合模式
1 parent 3b8933f commit 716f2dd

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.design.composite.v1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 创建 Employee 类,该类带有 Employee 对象的列表
8+
*
9+
* @author wliduo[i@dolyw.com]
10+
* @date 2023/1/31 14:07
11+
*/
12+
public class Employee {
13+
private String name;
14+
private String dept;
15+
private int salary;
16+
private List<Employee> subordinates;
17+
18+
public Employee(String name, String dept, int sal) {
19+
this.name = name;
20+
this.dept = dept;
21+
this.salary = sal;
22+
subordinates = new ArrayList<>();
23+
}
24+
25+
public void add(Employee e) {
26+
subordinates.add(e);
27+
}
28+
29+
public void remove(Employee e) {
30+
subordinates.remove(e);
31+
}
32+
33+
public List<Employee> getSubordinates() {
34+
return subordinates;
35+
}
36+
37+
public String toString() {
38+
return ("Employee :[ Name : " + name
39+
+ ", dept : " + dept + ", salary :"
40+
+ salary + " ]");
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.design.composite.v1;
2+
3+
/**
4+
* 组合模式
5+
*
6+
* 使用 Employee 类来创建和打印员工的树形层次结构
7+
*
8+
* @author wliduo[i@dolyw.com]
9+
* @date 2023/1/31 14:07
10+
*/
11+
public class Main {
12+
13+
public static void main(String[] args) {
14+
Employee Ceo = new Employee("王明", "CEO", 30000);
15+
16+
Employee headSales = new Employee("销售主管", "销售部门", 20000);
17+
18+
Employee headMarketing = new Employee("营销主管", "营销部门", 20000);
19+
20+
Employee clerk1 = new Employee("文员1", "营销部门", 10000);
21+
Employee clerk2 = new Employee("文员2", "营销部门", 10000);
22+
23+
Employee sales1 = new Employee("销售1", "销售部门", 10000);
24+
Employee sales2 = new Employee("销售2", "销售部门", 10000);
25+
26+
Ceo.add(headSales);
27+
Ceo.add(headMarketing);
28+
29+
headSales.add(sales1);
30+
headSales.add(sales2);
31+
32+
headMarketing.add(clerk1);
33+
headMarketing.add(clerk2);
34+
35+
// 打印该组织的所有员工
36+
System.out.println(Ceo);
37+
for (Employee headEmployee : Ceo.getSubordinates()) {
38+
System.out.println(headEmployee);
39+
for (Employee employee : headEmployee.getSubordinates()) {
40+
System.out.println(employee);
41+
}
42+
}
43+
}
44+
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.design.composite.v2;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 分支节点
8+
*
9+
* @author wliduo[i@dolyw.com]
10+
* @date 2023/1/31 14:15
11+
*/
12+
class BranchNode extends Node {
13+
List<Node> nodes = new ArrayList<>();
14+
15+
String name;
16+
17+
public BranchNode(String name) {
18+
this.name = name;
19+
}
20+
21+
@Override
22+
public void p() {
23+
System.out.println(name);
24+
}
25+
26+
public void add(Node n) {
27+
nodes.add(n);
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.design.composite.v2;
2+
3+
/**
4+
* 叶子节点
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/1/31 14:15
8+
*/
9+
public class LeafNode extends Node {
10+
11+
String content;
12+
13+
public LeafNode(String content) {
14+
this.content = content;
15+
}
16+
17+
@Override
18+
public void p() {
19+
System.out.println(content);
20+
}
21+
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.design.composite.v2;
2+
3+
/**
4+
* 组合模式
5+
*
6+
* 模拟树形层次结构输出
7+
*
8+
* @author wliduo[i@dolyw.com]
9+
* @date 2023/1/31 14:07
10+
*/
11+
public class Main {
12+
13+
public static void main(String[] args) {
14+
15+
BranchNode root = new BranchNode("章节");
16+
BranchNode chapter1 = new BranchNode("章节1");
17+
BranchNode chapter2 = new BranchNode("章节2");
18+
Node r1 = new LeafNode("尾声");
19+
Node c11 = new LeafNode("小结1-1");
20+
Node c12 = new LeafNode("小结1-2");
21+
BranchNode b21 = new BranchNode("选读1");
22+
Node c211 = new LeafNode("选读1小结1");
23+
Node c212 = new LeafNode("选读1小结2");
24+
25+
root.add(chapter1);
26+
root.add(chapter2);
27+
root.add(r1);
28+
chapter1.add(c11);
29+
chapter1.add(c12);
30+
chapter2.add(b21);
31+
b21.add(c211);
32+
b21.add(c212);
33+
34+
tree(root, 0);
35+
36+
}
37+
38+
public static void tree(Node b, int depth) {
39+
for (int i = 0; i < depth; i++) {
40+
System.out.print("--");
41+
}
42+
43+
b.p();
44+
45+
if (b instanceof BranchNode) {
46+
for (Node n : ((BranchNode) b).nodes) {
47+
tree(n, depth + 1);
48+
}
49+
}
50+
}
51+
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.design.composite.v2;
2+
3+
/**
4+
* 节点
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/1/31 14:15
8+
*/
9+
abstract public class Node {
10+
11+
abstract public void p();
12+
13+
}

0 commit comments

Comments
 (0)