hooyantsing's Blog

P78 组合模式(2)-院校展示

字数统计: 450阅读时长: 2 min
2020/11/10

尚硅谷Java设计模式(图解+框架源码剖析)

案例 院校展示

1
2
3
4
5
6
7
8
9
10
classDiagram
class OrganizationComponet
class University
class College
class Department
OrganizationComponet <|-- University
OrganizationComponet <|-- College
OrganizationComponet <|-- Department
University o-- College
College o-- Department

树节点定义,共同特征。

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
public abstract class OrganizationComponet {
private String name;
private String des;

// add remove 默认方法,避免叶子节点也要实现方法
protected void add(OrganizationComponet o){
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComponet o){
throw new UnsupportedOperationException();
}
protected abstract void print();

public OrganizationComponet(String name, String des) {
this.name = name;
this.des = des;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDes() {
return des;
}

public void setDes(String des) {
this.des = des;
}
}

非叶子节点

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
public class University extends OrganizationComponet {
private List<OrganizationComponet> OrganizationComponets = new ArrayList<>();

public University(String name, String des) {
super(name, des);
}

@Override
protected void add(OrganizationComponet o) {
OrganizationComponets.add(o);
}

@Override
protected void remove(OrganizationComponet o) {
OrganizationComponets.remove(o);
}

@Override
protected void print() {
System.out.println(this.getName());
for(OrganizationComponet o : OrganizationComponets){
o.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
public class College extends OrganizationComponet {
private List<OrganizationComponet> OrganizationComponets = new ArrayList<>();

public College(String name, String des) {
super(name, des);
}

@Override
protected void add(OrganizationComponet o) {
OrganizationComponets.add(o);
}

@Override
protected void remove(OrganizationComponet o) {
OrganizationComponets.remove(o);
}

@Override
protected void print() {
System.out.println(this.getName());
for(OrganizationComponet o : OrganizationComponets){
o.print();
}
}
}

叶子节点

1
2
3
4
5
6
7
8
9
10
11
// 叶子节点
public class Department extends OrganizationComponet {
public Department(String name, String des) {
super(name, des);
}

@Override
protected void print() {
System.out.println(this.getName());
}
}

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Client {
public static void main(String[] args) {
// 创建大学
OrganizationComponet university = new University("湖南工学院","hnit");
// 创建二级学院
OrganizationComponet computerCollege = new College("计算机学院","jisuanji");
OrganizationComponet infoEngineerCollege = new College("信息学院","xinxi");
// 创建专业
OrganizationComponet network = new Department("网络工程","wangluogongcheng");
OrganizationComponet software = new Department("软件工程","ruanjiangongcheng");

// 套娃
university.add(computerCollege);
university.add(infoEngineerCollege);
computerCollege.add(network);
computerCollege.add(software);

// 测试:
university.print();
System.out.println("--------------------");
computerCollege.print();
}
}
CATALOG
  1. 1. 案例 院校展示