组合模式


组合模式

1、学校院系展示需求

编写程序展示一个学校院系结构:

需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。如图:

img

传统方式解决学校院系展示(类图)

img

问题分析

  • 1)将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层次的

  • 2)实际上我们的要求是:在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。因此这种方案,不能很好实现的 管理 的操作,比如对学院、系的添加、删除、遍历等

  • 3)解决方案:把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树形结构,可以更好的实现管理操作 ==> 组合模式

2、组合模式基本介绍

  • 1)组合模式(Composite Pattern),又叫部分整体模式。它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系

  • 2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次

  • 3)这种类型的设计模式属于结构型模式

  • 4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象

原理类图

img

对原理结构图的说明一即组合模式的角色及职责

  • 1)Component:这是组合中对象声明接口。在适当情况下,实现所有类共有的接口默认行为,用于访问和管理 Component子部件。Component可以是抽象类或者接口

  • 2)Leaf:在组合中表示叶子结点,叶子结点没有子节点

  • 3)Composite:非叶子结点,用于存储子部件,在Component接口中实现子部件的相关操作。比如增加、删除

解决的问题

组合模式解决这样的问题,当我们的要处理的对象可以生成一棵树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子

img

3、组合模式解决学校院系展示

UML 类图

img

核心代码

Component 抽象类 OrganizationComponent

// Component 抽象类
public abstract class OrganizationComponent {
    private String name;

    public OrganizationComponent(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void add(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    public void remove(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    public abstract void print();
}

Composite 非叶子节点 University

// Composite 非叶子节点
public class University extends OrganizationComponent {

    List<OrganizationComponent> organizationComponentList = new ArrayList<>();

    public University(String name) {
        super(name);
    }

    @Override
    public void add(OrganizationComponent organizationComponent) {
        organizationComponentList.add(organizationComponent);
    }

    @Override
    public void remove(OrganizationComponent organizationComponent) {
        organizationComponent.remove(organizationComponent);
    }

    @Override
    public void print() {
        for (OrganizationComponent organizationComponent : organizationComponentList) {
            organizationComponent.print();
        }
    }
}

Colleage

public class College extends OrganizationComponent{

    List<OrganizationComponent> organizationComponentList = new ArrayList<>();

    public College(String name) {
        super(name);
    }

    @Override
    public void add(OrganizationComponent organizationComponent) {
        organizationComponentList.add(organizationComponent);
    }

    @Override
    public void remove(OrganizationComponent organizationComponent) {
        organizationComponent.remove(organizationComponent);
    }

    @Override
    public void print() {
        System.out.println("=============" + getName() + "=============");
        for (OrganizationComponent organizationComponent : organizationComponentList) {
            organizationComponent.print();
        }
    }
}

leaf 叶子节点major

// Leaf 叶子结点
public class Major extends OrganizationComponent {
    public Major(String name) {
        super(name);
    }

    @Override
    public void print() {
        System.out.println(getName());
    }
}

客户端 Client

public class Client {
    public static void main(String[] args) {
        //大学
        OrganizationComponent university = new University("清华大学");
        //学院
        OrganizationComponent computerCollege = new College("计算机学院");
        OrganizationComponent infoEngineerCollege = new College("信息工程学院");
        //专业
        computerCollege.add(new Major("软件工程"));
        computerCollege.add(new Major("网络工程"));
        computerCollege.add(new Major("计算机科学与技术"));
        infoEngineerCollege.add(new Major("通信工程"));
        infoEngineerCollege.add(new Major("信息工程"));

        university.add(computerCollege);
        university.add(infoEngineerCollege);
        university.print();
    }
}
=============计算机学院=============
软件工程
网络工程
计算机科学与技术
=============信息工程学院=============
通信工程
信息工程

4、HashMap源码分析

Java 的集合类—— HashMap 就使用了组合模式

UML 类图

img

核心代码

// Component
public interface Map<K,V> {
    interface Entry<K,V> {}
}
public abstract class AbstractMap<K,V> implements Map<K,V> {}
// Composite
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
    // Leaf
    static class Node<K,V> implements Map.Entry<K,V> {}
}

Author: qwq小小舒
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source qwq小小舒 !
  TOC