关于java:我所知道设计模式之组合模式

2次阅读

共计 4384 个字符,预计需要花费 11 分钟才能阅读完成。

前言介绍


接下里介绍的是 Java 的设计模式之一:组合模式

咱们还是以一个问题进行开展,引入组合模式

编写程序展现学校院系构造:

要求:在页面中展现出 学校的院系组成

一个学校 有多个学院 ,一个学院 有多个系

一、传统形式解决问题

个别咱们的思路是:先写一个学校、学校下有学院、学院下有系

但咱们想一想,学校与学院是继承关系吗?学院与系又是吗?

咱们精确的关系应该是组合关系:学院蕴含系,学校蕴含学院

传统计划解决学校院系展现存在的问题剖析

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

实际上咱们的要求是:在页面中展现出 学校的院系组成

一个学校 有多个学院 ,一个学院 有多个系

因而这种计划,不能很好实现的治理的操作,比方对学院、系的增加,删除,遍历等

解决方案:把学校、院、系都看做是组织构造,他们之间没有继承的关系,而是一个树形构造,能够更好的实现治理操作。

二、什么是组合模式

组合模式(Composite Pattern),又叫 局部整体模式

它创立了 对象组的树形构造,将对象组合成树状构造以示意“整体 - 局部”的档次 关系。

组合模式 根据树形构造来组合对象 ,用来示意 局部以及整体档次

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

组合模式使得用户对单个对象和组合对象的拜访具备一致性,即:组合能让客户以统一的形式解决个别对象以及组合对象

组合模式原理类图

  • Component : 这是组合中对象申明接口

在适当状况下,实现所有类共有的接口默认行为, 用于拜访和治理

  • Component 子部件, Component 能够是抽象类或者接口
  • Leaf : 在组合中示意叶子节点,叶子节点没有子节点
  • Composite : 非叶子节点,用于存储子部件

在 Component 接口中实现 子部件的相干操作,比方减少(add), 删除

组合模式解决的问题

当咱们的 要解决的对象能够生成一颗树形构造,而咱们要对树上的节点和叶子进行操作

它可能提供统一的形式,而不必思考它是节点还是叶子,对应的示意图

三、应用组合模式解决问题

咱们依照思路,县创立一个组织者类:OrganizationComponent

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) {super();
        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();}

接下来咱们实现非叶子节点学校的 Composite 实现

//University 就是 Composite ,  能够治理 College
class University extends OrganizationComponent {List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

    // 结构器
    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);
    }

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


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

    // print 办法,就是输入 University  蕴含的学院
    @Override
    protected void print() {System.out.println("--------------" + getName() + "--------------");
        // 遍历 organizationComponents
        for (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();
        }
    }
}

接下来咱们实现非叶子节点学院的 Composite 实现

//College  就是 Composite ,  能够治理 Department
class College  extends OrganizationComponent {List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

    // 结构器
    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);
    }

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


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

    // print 办法,就是输入 University  蕴含的学院
    @Override
    protected void print() {System.out.println("--------------" + getName() + "--------------");
        // 遍历 organizationComponents
        for (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();
        }
    }
}

接下来咱们编写叶子节点的系 left 实现

class Department extends OrganizationComponent {

    // 叶子节点没有汇合
    public Department(String name, String des) {super(name, des);
    }
    //add , remove 就不必写了,因为他是叶子节点
    @Override
    public String getName() {return super.getName();
    }

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

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

那么接下来咱们应用 demo 来看看组合模式下的样子

public static void main(String[] args) {

    // 从大到小创建对象 学校
    OrganizationComponent university = new University("清华大学", "中国顶级大学");

    // 创立 学院
    OrganizationComponent computerCollege = new College("计 算 机 学 院", "计 算 机 学 院");
    OrganizationComponent infoEngineercollege = new College("信息工程学院", "信息工程学院");

    // 计 算 机 学 院上面的系(业余)
    computerCollege.add(new Department("软件工程", "软件工程不错"));
    computerCollege.add(new Department("网络工程", "网络工程不错"));
    computerCollege.add(new Department("计算机科学与技术", "计算机科学与技术是老牌的业余"));

    // 信息工程学院上面的系(业余)
    infoEngineercollege.add(new Department("通信工程", "通信工程不好学"));
    infoEngineercollege.add(new Department("信息工程", "信息工程好学"));

    // 将学院退出到 学校
    university.add(computerCollege);
    university.add(infoEngineercollege);

    university.print();
    infoEngineercollege.print();}

运行后果如下:------- 清华大学 ----------
------- 计算机学院 ---------
计算机科学与技术
软件工程
网络工程
------- 信息工程学院 -------
通信工程
信息工程

参考资料


尚硅谷:设计模式(韩顺平老师):组合模式

Refactoring.Guru:《深刻设计模式》

正文完
 0