组合模式
基本介绍
- 组合模式,又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系。
- 组合模式依据树形结构来组合对象,用来表示部分以及整体层次。
- 这种类型的设计模式属于结构型模式。
- 组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能够让客户以一致的方式处理个别对象以及组合对象。
原理类图
对原理结构图说明即组合模式的角色及职责
- Component:这是组合模式中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或者接口
- Leaf:在组合中表示叶子节点,叶子节点没有子节点
- Composite:非叶子节点,用于存储子部件,在Component 接口中实现子部件的相关操作。
解决的问题
- 组合模式解决这样的问题,当我们的要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子。
- 对应的示例图
类图实例
代码实例
OrganizationComponent 抽象类
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() {
}
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 类
//University 就是Composite,可以管理College
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<>();
//构造器
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();
}
@Override
protected void print() {
System.out.println("----------" + getName() + "------------");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
College 类
public class College extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<>();
//构造器
public College(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();
}
@Override
protected void print() {
System.out.println("----------" + getName() + "------------");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
Department 类
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("清华大学", "中国顶级大学");
//创建学院
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();
}
}
jdk中的源码分析
putAll()这个方法使用了组合模式这一设计模式
public class Composite{
// 说明
// 1. Map就是一个抽象的构建(类似我们的Component)
// 2. HashMap是一个中间的构建(Composite),实现/继承了相关方法
// put,putall
// 3. Node是一个HashMap的静态内部类,类似Leaf叶子节点,这里就没有put,putAll
// static class Node<K,V> implements Map.Entry<K,V>
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(0, "东游记");//直接存放叶子节点(Node)
Map<Integer, String> map = new HashMap<>();
map.put(1, "西游记");
map.put(2, "红楼梦");
hashMap.putAll(map);
System.out.println(hashMap);
}
AbstractMap中包含putAll()方法,并且AbstractMap是Map的一个实现类.但是AbstractMap本身也是一个抽象类,其抽象方法应有其子类去实现。
HashMap本身继承了AbstractMap并实现了Map接口,是对组合模式中Component的一个具体子类实现。
叶子节点在哪里?静态的Node可以近似的看作一个叶子节点。
HashMap的类图实例
组合模式的注意事项和细节
- 简化客户操作,客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。
- 具有较强的扩展性,当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做任何修改。
- 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
- 需要遍历组织结构,或者处理的对象具有树形结构时,非常适合使用组合模式。
- 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式。