23种设计模式——组合模式
  tlTTizG0s3gr 2023年12月12日 67 0

组合模式

今天来学习23种设计模式的组合模式,组合模式主要用于组合多个对象形成树形结构以表示“整体-部分”的结构层次。

概念:

组合模式(Composite Pattern)又叫作部分-整体模式,它是一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型设计模式。

特点:

优点:

  • 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码。
  • 容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”。

缺点:

  • 在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
代码:
package cn.ppdxzz.composite;
/**
 * Description:抽象类,实现所有类共有的接口默认行为
 */
public abstract class OrganizationComponent {
    private String name;//名称
    private String description;//描述

    protected void add(OrganizationComponent organizationComponent) {
        //默认实现
        throw new UnsupportedOperationException();
    }
    protected void remove(OrganizationComponent organizationComponent) {
        //默认实现
        throw new UnsupportedOperationException();
    }
    //抽象方法,子类都需实现
    protected abstract void print();

    public OrganizationComponent() {
    }

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

    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;
    }
}

系别

package cn.ppdxzz.composite;

/**
 * Description:系别
 */
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());
    }
}

学院

package cn.ppdxzz.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:学院
 */
public class College extends OrganizationComponent {
    //List集合用于存放系别
    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    public College(String name,String description) {
        super(name, description);
    }
    //添加系别
    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }
    //移除系别
    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        organizationComponents.remove(organizationComponent);
    }
    //输出系别信息
    @Override
    protected void print() {
        System.out.println("---" + getName() + "---");
        organizationComponents.forEach(OrganizationComponent::print);
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getDescription() {
        return super.getDescription();
    }
}

学校

package cn.ppdxzz.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:学校
 */
public class University extends OrganizationComponent {
    //List集合存放College学院
    List<OrganizationComponent> organizationComponents = new ArrayList<>();
    public University(String name,String description) {
        super(name, description);
    }
    //添加学院
    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }
    //删除学院
    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        organizationComponents.remove(organizationComponent);
    }
    //输出学院信息
    @Override
    protected void print() {
        System.out.println("---" + getName() + "---");
        organizationComponents.forEach(OrganizationComponent::print);
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getDescription() {
        return super.getDescription();
    }
}

测试

package cn.ppdxzz.composite;

/**
 * Description:组合模式(学校院系展示问题)
 */
public class Client {
    public static void main(String[] args) {
        //创建学校
        OrganizationComponent university = new University("大学","大学很美");
        //创建学院
        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();



    }
}
总结:
  • 简化客户端的操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。
  • 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动。
  • 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式。
  • 组合模式要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式。
  • Java 的集合类 HashMap 中就使用到了组合模式。

虽然组合模式能够清晰地定义分层次的复杂对象,也使得增加新构件也更容易,但是这样就导致了系统的设计变得更加抽象,如果系统的业务规则比较复杂的话,使用组合模式就有一定的挑战了。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月12日 0

暂无评论

推荐阅读
tlTTizG0s3gr