开始介绍之前,先分析一个实例:
编写一个程序展示一个学校院系结构,需要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系:如下:
——————**大学**————————
——————计算机学院———————
计算机科学与技术
软件工程
网络工程
——————信息工程学院————————
通信工程
信息工程
针对上述实例,很容易想到写一个学校类,一个学院类继承学校类,一个专业类继承学院类。对此方法进行分析:
(1)将学院看作是学校的子类,系是学院的子类,实际上是站在组织大小进行分层的,这种方案不能实现较好的管理操作,比如对学院、系的添加删除,遍历等。
(2)解决方案:将学校、院、系都看作是组织结构,他们之间没有继承关系,而是树形结构,可以更好的实现管理操作,即使用组合模式。
(1)组合模式,又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体—部分”的层次关系。
(2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次。
(3)这种类型的设计模式属于树形结构模式。
(4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象。
原理图:
说明:
(1)Component:这是组合中对象声明接口,在适当的情况下,实现所有类共有的接口默认行为。用于访问和管理Component子部件,可以是抽象类或者接口。
(2)Leaf:在组合中表示叶子节点,叶子节点没有子节点
(3)Composite:在组合中表示非叶子节点,用于存储子部件,在Composite中实现子部件的相关操作,比如增加,删除。
上述实例使用组合模式实现:
// Component
public abstract class OrganizationComponent {
private String name;
private String description;
public OrganizationComponent(String name, String description) {
this.name = name;
this.description = description;
}
// 不是所有的子类都需要add,remove,因此提供默认的实现
protected void add(OrganizationComponent organizationComponent) {}
protected void remove(OrganizationComponent organizationComponent) {}
// 所有的子类都需要输出信息,因此做成抽象方法
protected abstract void print();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
// 相当于Composite,可以管理College
public class University extends OrganizationComponent {
List organizationComponentLists = new ArrayList<>();
public University(String name, String description) {
super(name, description);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponentLists.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponentLists.remove(organizationComponent);
}
@Override
protected void print() {
System.out.println("---------------" + getName() + "----------------");
for (OrganizationComponent organization : organizationComponentLists) {
organization.print();
}
}
}
public class College extends OrganizationComponent {
List organizationComponentLists = new ArrayList<>();
public College(String name, String description) {
super(name, description);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponentLists.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponentLists.remove(organizationComponent);
}
@Override
protected void print() {
System.out.println("-----" + getName());
for (OrganizationComponent organization : organizationComponentLists) {
organization.print();
}
}
}
public class Department extends OrganizationComponent {
public Department(String name, String description) {
super(name, description);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
@Override
protected void print() {
System.out.println(getName());
}
}
// 主方法
public static void main(String[] args) {
University university = new University("***大学", "NB大学");
College computerCollege = new College("计算机学院", "计算机学院");
College infoEngineerCollege = new College("信息工程学院", "信息工程");
computerCollege.add(new Department("计算机科学与技术", "计科"));
computerCollege.add(new Department("软件工程", "软件工程"));
infoEngineerCollege.add(new Department("通信工程", "通信"));
infoEngineerCollege.add(new Department("电子信息", "电子信息"));
university.add(computerCollege);
university.add(infoEngineerCollege);
university.print();
}
// 运行结果
/**
---------------***大学----------------
-----计算机学院
计算机科学与技术
软件工程
-----信息工程学院
通信工程
电子信息
*/
(1)JDK中HashMap使用了组合模式。简单介绍:
1)Map就是一个抽象的构建(类似Component)
2)HashMap是一个中间的构建(Composite),实现/继承了相关put,putAll等方法。
3)Node是HashMap的静态内部类,类似leaf叶子节点。
(2)简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。
(3)具有较强的扩展性。当我们需要改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动。
(4)方便创建出复杂的层析结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构。
(5)需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式。
(6)需要较高的抽象思维,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式。