介绍:
- 组合模式又叫部分-整体模式,他创建对象组的树形结构,将对象组合成树状结构以表示,**“整体-部分”**的层次关系。
- 组合模式依据树形结构来组合对象,用来表示部分以及整体的层次。
- 组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一种的方式处理个别对象以及组合对象。
主要角色说明:
- Component:这是组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,可以是抽象类或者是接口。
- leaf:叶子节点,叶子节点没有子节点。
- Composite:非叶子节点,用于存储子部件,在Component中实现子部件的相关操作,比如新增、删除等。
解决问题:
组合模式解决这样的问题,当我们要处理的对象可以生产一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑他是节点还是叶子。
需求:展示打印一个院系的组成结构。
public abstract class OrganizationCompontent {
private String name;
private String des;
protected void add(OrganizationCompontent organizationCompontent){
//默认实现
throw new UnsupportedOperationException();
}
protected void remove(OrganizationCompontent organizationCompontent){
//默认实现
throw new UnsupportedOperationException();
}
public OrganizationCompontent(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;
}
//方法子类都需要实现
protected abstract void print();
}
/**
* 叶子节点
*/
public class Department extends OrganizationCompontent {
public Department(String name, String des) {
super(name, des);
}
@Override
public String getDes() {
return super.getDes();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println(getName());
}
}
/**
* 非叶子节点
*/
public class College extends OrganizationCompontent {
List<OrganizationCompontent> organizationCompontents = new ArrayList<>();
public College(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationCompontent organizationCompontent) {
organizationCompontents.add(organizationCompontent);
}
@Override
protected void remove(OrganizationCompontent organizationCompontent) {
organizationCompontents.remove(organizationCompontent);
}
@Override
protected void print() {
System.out.println("----------------" + getName() + "-------------------");
for (OrganizationCompontent compontent: organizationCompontents) {
compontent.print();
}
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
}
/**
* 非叶子节点
*/
public class Univerty extends OrganizationCompontent {
List<OrganizationCompontent> organizationCompontents = new ArrayList<>();
public Univerty(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationCompontent organizationCompontent) {
organizationCompontents.add(organizationCompontent);
}
@Override
protected void remove(OrganizationCompontent organizationCompontent) {
organizationCompontents.remove(organizationCompontent);
}
@Override
protected void print() {
System.out.println("----------------" + getName() + "-------------------");
for (OrganizationCompontent compontent: organizationCompontents) {
compontent.print();
}
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
}
public class Client {
public static void main(String[] args) {
OrganizationCompontent univerty = new Univerty("清华大学", "Num.1");
OrganizationCompontent college = new College("计算机学院", "计算机学院");
OrganizationCompontent college1 = new College("信息学院", "信息学院");
college.add(new Department("软件工程","软件工程不错"));
college.add(new Department("网络工程","网络工程不错"));
college.add(new Department("计算机科学与技术","计算机科学与技术不错"));
college1.add(new Department("信息工程","信息工程不错"));
college1.add(new Department("电子信息","电子信息不错"));
college1.add(new Department("微电子","微电子不错"));
univerty.add(college);
univerty.add(college1);
univerty.print();
college.print();
}
}
组合模式在JDK源码中的使用:HashMap使用到了组合模式
说明:
- Map就是抽象的构建,类似我们的Component
- HashMap是一个中间的构建,即composite,实现或继承了相关方法
- Node是HashMap的静态内部类,类似leaf叶子节点,这里就没有put、putAll方法
组合模式注意实现和细节:
- 简化客户端的操作,客户端只需要面对一致的对象而不用考虑整体部分或者叶子节点的问题。
- 具有较强的扩展性,当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不需要做出任何改变
- 方便创建出复杂的层次结构,客户端不用理会组合里面的组成细节,容易添加节点或者叶子节点而创建出复杂的树形结构
- 需要遍历组织结构,或者处理的对象具有树形结构时,非常适合使用组合模式
- 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式