学校院系展示需求
编写程序展示一个学校院系结构:
需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。如图:
传统方式解决学校院系展示(类图)
问题分析
- 1)将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层次的
- 2)实际上我们的要求是:在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。因此这种方案,不能很好实现的 管理 的操作,比如对学院、系的添加、删除、遍历等
- 3)解决方案:把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树形结构,可以更好的实现管理操作 ==> 组合模式
组合模式基本介绍
- 1)组合模式(Composite Pattern),又叫部分整体模式。它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系
- 2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次
- 3)这种类型的设计模式属于结构型模式
- 4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象
原理类图
对原理结构图的说明一即组合模式的角色及职责
- 1)
Component
:这是组合中对象声明接口。在适当情况下,实现所有类共有的接口默认行为,用于访问和管理 Component
子部件。Component
可以是抽象类或者接口
- 2)
Leaf
:在组合中表示叶子结点,叶子结点没有子节点
- 3)
Composite
:非叶子结点,用于存储子部件,在Component
接口中实现子部件的相关操作。比如增加、删除
解决的问题
组合模式解决这样的问题,当我们的要处理的对象可以生成一棵树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子
组合模式解决学校院系展示
UML 类图
核心代码
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
| public abstract class OrganizationComponent { private String name;
public OrganizationComponent(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public void add(OrganizationComponent organizationComponent) { throw new UnsupportedOperationException(); }
public void remove(OrganizationComponent organizationComponent) { throw new UnsupportedOperationException(); }
public abstract void print(); }
public class University extends OrganizationComponent { List<OrganizationComponent> organizationComponentList = new ArrayList<>();
public University(String name) { super(name); }
@Override public void add(OrganizationComponent organizationComponent) { organizationComponentList.add(organizationComponent); }
@Override public void remove(OrganizationComponent organizationComponent) { organizationComponent.remove(organizationComponent); }
@Override public void print() { for (OrganizationComponent organizationComponent : organizationComponentList) { organizationComponent.print(); } } } public class College extends OrganizationComponent { List<OrganizationComponent> organizationComponentList = new ArrayList<>();
public College(String name) { super(name); }
@Override public void add(OrganizationComponent organizationComponent) { organizationComponentList.add(organizationComponent); }
@Override public void remove(OrganizationComponent organizationComponent) { organizationComponent.remove(organizationComponent); }
@Override public void print() { System.out.println("=============" + getName() + "============="); for (OrganizationComponent organizationComponent : organizationComponentList) { organizationComponent.print(); } } }
public class Major extends OrganizationComponent { public Major(String name) { super(name); }
@Override public void print() { System.out.println(getName()); } }
public class Client { public static void main(String[] args) { OrganizationComponent university = new University("清华大学"); OrganizationComponent computerCollege = new College("计算机学院"); OrganizationComponent infoEngineerCollege = new College("信息工程学院"); computerCollege.add(new Major("软件工程")); computerCollege.add(new Major("网络工程")); computerCollege.add(new Major("计算机科学与技术")); infoEngineerCollege.add(new Major("通信工程")); infoEngineerCollege.add(new Major("信息工程"));
university.add(computerCollege); university.add(infoEngineerCollege); university.print(); } }
|
JDK 源码分析
Java 的集合类—— HashMap 就使用了组合模式
UML 类图
核心代码
1 2 3 4 5 6 7 8 9 10
| public interface Map<K,V> { interface Entry<K,V> {} } public abstract class AbstractMap<K,V> implements Map<K,V> {}
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable { static class Node<K,V> implements Map.Entry<K,V> {} }
|
说明
- 1)Map 就是一个抽象的构建,类似
Component
- 2)HashMap 是一个中间的构建,类似
Composite
,实现 / 继承了相关方法 put、putAll
- 3)Node 是 HashMap 的静态内部类,类似
Leaf
叶子节点,这里就没有 put
注意事项和细节
- 1)简化客户端操作:客户端只需要面对一致的对象,而不用考虑整体部分或者节点叶子的问题
- 2)具有较强扩展性:当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动
- 3)方便创建复杂的层次结构:客户端不用理会组合里面的组成细节,容易添加节点或者叶子,从而创建出复杂的树形结构
- 4)需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
- 5)要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式