Java设计模式(九)——————【结构型模式】设计模式之组合模式

源码地址:https://github.com/877148107/java-design-pattern

  • 基本介绍

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

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

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

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

  • 基本原理

1)Component:这是组合中对象声明接口,实现所有类公有的接口默认行为,用于访问和管理子类部件,component可以是一个抽象类也可以是一个接口。

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

3)Composite:非叶节点,用于存储子部件,实现component的行为方法,如果add、remove。。。。

在这里插入图片描述

使用场景:

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

在这里插入图片描述

  • 案例说明

    1、需求

    编写程序展示一个学校院系结构:要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。

    2、UML类图

在这里插入图片描述

也可以是这样:
在这里插入图片描述

3、代码实现

​ Component抽象类或者接口,包含了公共的行为。

public abstract class OrganizationComponent {

    private String name;

    private String desc;

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

    /**
     * 子类实现
     */
    public abstract void print();

    /**
     * 添加  默认实现
     * @param organizationComponent
     */
    protected void add(OrganizationComponent organizationComponent){
        throw new UnsupportedOperationException();
    }

    /**
     * 删除  默认实现
     * @param organizationComponent
     */
    protected void remove(OrganizationComponent organizationComponent){
        throw new UnsupportedOperationException();
    }

    public String getName() {
        return name;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

Composite非叶节点存储子部件

public class University extends OrganizationComponent {

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

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

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }

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

    @Override
    public void print() {
        //打印学校
        System.out.println("=================="+getName()+"========================="+getDesc());
        //打印学校院系
        organizationComponents.stream().forEach(e->e.print());
    }
}
public class College extends OrganizationComponent {

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

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

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }

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

    @Override
    public void print() {
        //学院学习
        System.out.println("----->>>>"+getName()+":"+getDesc());
        //打印院系
        organizationComponents.stream().forEach(e->e.print());
    }
}

leaf叶节点

public class Department extends OrganizationComponent {
    public Department(String name, String desc) {
        super(name, desc);
    }

    @Override
    public void print() {
        //打印院系名称
        System.out.println(getName());
    }
}

测试:

public class OrganizationClient {

    public static void main(String[] args) {
        OrganizationComponent university = new University("清华大学", "66666");

        OrganizationComponent college1 = new College("计算机学院","计算机学院");
        OrganizationComponent college2 = new College("信息工程学院","信息工程学院");

        college1.add(new Department("软件工程","电子商务"));
        college1.add(new Department("游戏策划","游戏策划"));

        college2.add(new Department("通信工程","通信工程"));
        college2.add(new Department("信息工程","信息工程"));

        university.add(college1);
        university.add(college2);

        university.print();
    }
}

在这里插入图片描述

  • JDK源码分析

    组合模式在JDK集合的源码分析,HashMap就使用了组合模式
    1.Map就是一个抽象的构建,这里是一个接口。类似案例里面的OrganizationComponent
    2.HashMap是一个中间构建实现相关的方法,put、putAll。。。。,类似案例里面的University、College
    3.Node是HashMap的静态内部类,类似案例里面的leaf叶节点Department,没有重写父类的put、putAll方法
    在这里插入图片描述

public class OrganizationClient {

    public static void main(String[] args) {
        //跟下面的案例类似
        //1.Map就是一个抽象的构建,这里是一个接口。类似案例里面的OrganizationComponent
        //2.HashMap是一个中间构建实现相关的方法,put、putAll。。。。,类似案例里面的University、College
        //3.Node是HashMap的静态内部类,类似案例里面的leaf叶节点Department,
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(0,"清华大学");

        Map<Integer,String> map1 = new HashMap<Integer,String>();
        map1.put(1,"北京大学");
        map1.put(2,"复旦大学");
        map.putAll(map1);
        System.out.println(map.toString());


//        OrganizationComponent university = new University("清华大学", "66666");
//
//        OrganizationComponent college1 = new College("计算机学院","计算机学院");
//        OrganizationComponent college2 = new College("信息工程学院","信息工程学院");
//
//        college1.add(new Department("软件工程","电子商务"));
//        college1.add(new Department("游戏策划","游戏策划"));
//
//        college2.add(new Department("通信工程","通信工程"));
//        college2.add(new Department("信息工程","信息工程"));
//
//        university.add(college1);
//        university.add(college2);
//
//        university.print();
    }
}
  • 总结

1)简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。

2)具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动.

3) 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构

4)需要遍历组织机构,或者处理的对象具有树形结构时, 非常适合使用组合模式.

5)要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值