组合模式(结构型)
设计模式的3个分类:创建型模式、结构型模式、行为型模式
组合模式定义:
组合模式,又叫部分整体模式,是指将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得对象对单个对象和组合对象对使用具有一致性(把一组相似的对象当作一个单一的对象)。
组合模式图示:

代码实现:
/**
* 这里也可以是接口(根据实际问题来)
*/
public abstract class OrganizationComponent {
private String name;
private String des;
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;
}
//不抽象(因为有叶子节点)
protected void add(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}
//打印(抽象方法)
protected abstract void print();
}
//类似树的根节点
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);
}
//print方法,输出University包含的学院
@Override
protected void print() {
System.out.println("----------------------" + getName() + "----------------------");
for (OrganizationComponent oc : organizationComponents) {
oc.print();
}
}
}
//类似树的中间节点
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);
}
//print方法,输出College包含的系
@Override
protected void print() {
System.out.println("------------" + getName() + "------------");
for (OrganizationComponent oc : organizationComponents) {
oc.print();
}
}
}
//类似树的叶子节点
public class Department extends OrganizationComponent{
//叶子节点,这里面不再有集合
public Department(String name, String des) {
super(name, des);
}
@Override
protected void print() {
System.out.println(getName());
}
}
/**
* 组合模式
*/
public class CompositePatternDemo {
public static void main(String[] args) {
//create university
OrganizationComponent university = new University("Tsinghua University", "Top University");
//create college
OrganizationComponent computerCollege = new College("计算机学院", "学院000");
OrganizationComponent infoEngineerCollege = new College("信息工程学院", "学院111");
//create department and add to college
computerCollege.add(new Department("软件工程", "AAA"));
computerCollege.add(new Department("网络工程", "BBB"));
computerCollege.add(new Department("计算机科学与技术", "CCC"));
//create department and add to college
infoEngineerCollege.add(new Department("通信工程", "DDD"));
infoEngineerCollege.add(new Department("信息工程", "EEE"));
//add college to university
university.add(computerCollege);
university.add(infoEngineerCollege);
university.print();
// computerCollege.print();
// infoEngineerCollege.print();
}
}
应用:
在Java中,Map、HashMap的使用是运用了“组合模式”;
在实际的开发中,可以结合实际情况,进行抽象类或接口的设计(非必须),进而使用组合模式。

浙公网安备 33010602011771号