介绍
又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构,属于结构型对象,使得用户对单个对象和组合对象的访问具有一致性,即组合能让客户以一致的方式处理个别对象以及组合对象。
uml图和代码
Component:这是组合中对象声明接口,在适当的情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或者接口,如OrganizationComponent。
Leaf:在组合中表叶子节点,叶子节点没有子节点,如Department
Composite:非叶子节点,用于存储子部件,在Component接口中实现子部件的相关操作,比如add,remove,如University和College,College存储Department,University存储College。
代码
Client类
public class Client {
public static void main(String[] args) {
//创建学校
OrganizationComponent university = new University("哈佛大学", "最好的大学");
//创建学院
OrganizationComponent ComputerCollege = new College("计算机学院", "计算机学院");
OrganizationComponent ArtCollege = new College("美术学院", "美术学院");
ComputerCollege.add(new Department("软件工程", "学费很贵"));
ComputerCollege.add(new Department("计算机科学与技术", "可以报师范专业"));
ArtCollege.add(new Department("室内设计", "工作不好找"));
ArtCollege.add(new Department("动画设计","工资很高"));
university.add(ComputerCollege);
university.add(ArtCollege);
university.print();
}
}
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(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;
}
//方法print ,因为子类都需要实现,所以做成抽象方法
protected abstract void print();
}
University类
//University 就是 Composite ,可以管理College
public class University extends OrganizationComponent{
//存放College
List<OrganizationComponent> organizationComponents = new ArrayList<>();
public University(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getDes() {
return super.getDes();
}
@Override
public String getName() {
return super.getName();
}
//输出University 包含的College
@Override
protected void print() {
System.out.println("-----"+getName()+"-------");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
College类
public class College extends OrganizationComponent {
//存放department
List<OrganizationComponent> organizationComponents = new ArrayList<>();
public College(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getDes() {
return super.getDes();
}
@Override
public String getName() {
return super.getName();
}
//输出University 包含的College
@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);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println(getName());
}
}
HashMap
uml图
- HashMap也是运用了组合模式。
- Map就是一个类似Component的接口。
- HashMap是一个Composite,实现和继承了相关方法如put,putall,是一个非叶子节点。
- Node是HashMap的静态内部类,类似Lead叶子节点,这里没有put,putall那些方法。static class Node<K,V> implement Map.Entry<K,V>。