假如我们有这样一个需求:需要展示一个学校想院系结构,一个学校下面有多个学院,一个学院下面有多个系:
XX大学
计算机学院
- 计算机科学与技术
- 软件工程
- 网络工程
师范学院
- 中文系
- 外语系
传统的方案:系继承学院、学院继承学校。
但实际上系、学院、学校之间并非继承关系,而是组合关系(部分和整体的关系)。
组合模式简介
- 组合模式又称部分整体模式,它创建了对象组的树形结构,将对象组合成树形结构以表示“整体-部分”的层次关系
- 组合模式依据树形结构来组合对象,用来表示部分以及整体层次
- 组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象
- 组合模式解决这样的问题,当我们要处理的对象可以生成一棵树形结构,而我们要对树上的节点进行操作时,它能够提供一致的方式,而不同考虑它是叶子节点还是非叶子节点
组合模式的角色及职责
- Component:为组合中对象的声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或者接口(如管理学校添加/删除学院、学院添加/删除系)
- Leaf:在组合中表示叶子节点,叶子节点没有子节点(比如系,系下面没有子组织机构)
- Composite:非叶子节点,用于存储子部件,在Component接口中实现子部件的相关操作(如学院,子部件是系;学校,子部件是学院)
实例
OrganizationComponent类即充当Component角色:
public abstract class OrganizationComponent {
private String name; // 名字
private String des; // 说明
protected void add(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}
//构造器
public OrganizationComponent(String name, String des) {
super();
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;
}
//方法print, 做成抽象的, 子类都需要实现
protected abstract void print();
}
子部件University即即充当Composite角色,管理College:
//大学
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
// 构造器
public University(String name, String des) {
super(name, des);
}
// 重写add
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
// 重写remove
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
// print方法,就是输出University 包含的学院
@Override
protected void print() {
System.out.println("--------------" + getName() + "--------------");
//遍历 organizationComponents
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
子部件College类即充当Composite角色,管理Department:
//学院
public class College extends OrganizationComponent {
//List 中 存放的Department
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
// 构造器
public College(String name, String des) {
super(name, des);
}
// 重写add
@Override
protected void add(OrganizationComponent organizationComponent) {
// 将来实际业务中,Colleage 的 add 和 University add 不一定完全一样
organizationComponents.add(organizationComponent);
}
// 重写remove
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
// print方法,就是输出University 包含的学院
@Override
protected void print() {
System.out.println("--------------" + getName() + "--------------");
//遍历 organizationComponents
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
子部件Department即充当Leaf角色,没有需要管理的子部件:
public class Department extends OrganizationComponent {
public Department(String name, String des) {
super(name, des);
}
//add , remove 就不用写了,因为他是叶子节点
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println(getName());
}
}
使用:
public class Client {
public static void main(String[] args) {
//学校
OrganizationComponent university = new University("XX大学", " 名牌大学 ");
//学院
OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 ");
OrganizationComponent infoEngineercollege = new College("师范学院", " 师范学院 ");
//创建各个学院下面的系(专业)
computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术 "));
computerCollege.add(new Department("软件工程", " 软件工程 "));
computerCollege.add(new Department("网络工程", " 网络工程 "));
infoEngineercollege.add(new Department(" 中文系", " 中文系 "));
infoEngineercollege.add(new Department(" 外语系", " 外语系 "));
//将学院加入到学校
university.add(computerCollege);
university.add(infoEngineercollege);
university.print();
// infoEngineercollege.print();
}
}
JDK源码中的组合模式
HashMap
使用了组合模式
比如下面这段代码:
Map<Integer,String> hashMap=new HashMap<Integer,String>();
hashMap.put(1, "a");
HashMap
类:
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> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
//...
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//...
}
AbstractMap
类:
public abstract class AbstractMap<K,V> implements Map<K,V> {
//...
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
//...
}
Map
接口:
public interface Map<K,V> {
//...
V put(K key, V value);
//...
}
Map
接口和AbstractMap
类属于Component角色,AbstractMap
类默认实现了put()
方法,HashMap
类继承AbstractMap
类并真正实现了put()
方法,HashMap
类属于Composite角色,静态内部类Node
属于Leaf角色,该类中无put()
方法。