一、需求说明
应用实例要求:
编写程序展示一个学校院系结构:要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。
二、组合模式应用
1、思路分析和图解
2、代码实现
Component 组件抽象类:
1 public abstract class OrganizationComponent {
2
3 private String name; //名字
4 private String des; //描述
5
6 /**
7 * 默认实现
8 */
9 protected void add(OrganizationComponent organizationComponent) {
10 throw new UnsupportedOperationException();
11 }
12
13 protected void remove(OrganizationComponent organizationComponent) {
14 throw new UnsupportedOperationException();
15 }
16
17 public OrganizationComponent(String name, String des) {
18 this.name = name;
19 this.des = des;
20 }
21
22 public String getName() {
23 return name;
24 }
25
26 public void setName(String name) {
27 this.name = name;
28 }
29
30 public String getDes() {
31 return des;
32 }
33
34 public void setDes(String des) {
35 this.des = des;
36 }
37
38 /**
39 * 打印信息,做成抽象的,所有的子类都需要实现
40 */
41 protected abstract void print();
42
43 }
Composite 子部件:
1 /**
2 * University 就是 Composite,可以管理 College
3 */
4 public class University extends OrganizationComponent{
5
6 List<OrganizationComponent> organizationComponents = new ArrayList<>();
7
8
9 public University(String name, String des) {
10 super(name, des);
11 }
12
13 /**
14 * 重写 add 方法
15 * @param organizationComponent
16 */
17 @Override
18 protected void add(OrganizationComponent organizationComponent) {
19 organizationComponents.add(organizationComponent);
20 }
21
22 @Override
23 protected void remove(OrganizationComponent organizationComponent) {
24 organizationComponents.remove(organizationComponent);
25 }
26
27 @Override
28 public String getName() {
29 return super.getName();
30 }
31
32 @Override
33 public String getDes() {
34 return super.getDes();
35 }
36
37 /**
38 * print 方法,就是输出 University 包含的学院
39 */
40 @Override
41 protected void print() {
42 System.out.println("----------"+ getName() +"------------");
43 //遍历 organizationComponents
44 for (OrganizationComponent component : organizationComponents) {
45 component.print();
46 }
47 }
48 }
49 -----------------------------------------------
50 /**
51 * 管理院系
52 */
53 public class College extends OrganizationComponent{
54 // 这个 List 中存放的 Department
55 List<OrganizationComponent> organizationComponents = new ArrayList<>();
56
57
58 public College(String name, String des) {
59 super(name, des);
60 }
61
62 /**
63 * 重写 add 方法
64 * @param organizationComponent
65 */
66 @Override
67 protected void add(OrganizationComponent organizationComponent) {
68 organizationComponents.add(organizationComponent);
69 }
70
71 @Override
72 protected void remove(OrganizationComponent organizationComponent) {
73 organizationComponents.remove(organizationComponent);
74 }
75
76 @Override
77 public String getName() {
78 return super.getName();
79 }
80
81 @Override
82 public String getDes() {
83 return super.getDes();
84 }
85
86 /**
87 * print 方法,就是输出 University 包含的院系
88 */
89 @Override
90 protected void print() {
91 System.out.println("----------"+ getName() +"------------");
92 //遍历 organizationComponents
93 for (OrganizationComponent component : organizationComponents) {
94 component.print();
95 }
96 }
97 }
Leaf 叶子类:
1 /**
2 * 院系
3 */
4 public class Department extends OrganizationComponent{
5
6
7 public Department(String name, String des) {
8 super(name, des);
9 }
10
11 //这里 add,remove 就不用写了,因为它是叶子节点
12
13
14 @Override
15 public String getDes() {
16 return super.getDes();
17 }
18
19 @Override
20 public String getName() {
21 return super.getName();
22 }
23
24
25 @Override
26 protected void print() {
27 System.out.println(getName());
28 }
29 }
客户端,测试类:
1 public class Client {
2 public static void main(String[] args) {
3
4 //从大到小创建对象,创建学校
5 OrganizationComponent university = new University("清华大学", "中国顶级大学");
6
7 //创建学院
8 OrganizationComponent computerCollege1 = new College("计算机学院", "计算机学院");
9 OrganizationComponent infoEngineerCollege2 = new College("信息工程学院", "信息工程学院");
10
11
12 //创建各个学院下面的专业
13 computerCollege1.add(new Department("软件工程", "软件工程"));
14 computerCollege1.add(new Department("网络工程", "网络工程"));
15 computerCollege1.add(new Department("计算机科学与技术", "计算机科学与技术"));
16
17 infoEngineerCollege2.add(new Department("通信工程", "通信工程"));
18 infoEngineerCollege2.add(new Department("信息工程", "信息工程"));
19
20 //将学院加入到学院中
21 university.add(computerCollege1);
22 university.add(infoEngineerCollege2);
23
24 university.print();
25
26 System.out.println("----------------------");
27 computerCollege1.print();
28
29 }
30 }
运行结果: