设为首页 加入收藏

TOP

组合模式(一)
2023-09-23 15:44:13 】 浏览:94
Tags:

组合模式

案例引入

编写程序,展示学校院系结构,要求在一个页面中展示出学院的院系组成,一个学院有几个学院,一个学院有几个系。如下图

传统方案及问题分析

传统方案

系 extends 学院 extends 学校

问题分析

  • 1.将学院看成学校的子类,系是学院的子类,这样实际上是按照组织大小进行划分层次的。
  • 2.这种传统方案的实现,不能实现管理,比如对学院的删除、遍历,对系得删除、遍历等。
  • 3.解决方案,将学校,学院,系都看作是组织机构,之间没有继承关系,而是一个树形结构,可以实现更好的管理。=> 使用组合模式。

基本介绍

  • 1.组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组得树形结构,将对象组合成树状结构,比表示"整体-部分"的层次关系。
  • 2.组合模式依据树形结构来组合对象,用来表示部分以及整体层次。
  • 3.属于结构性模式
  • 4.组合模式使用户对单个对象和组合对象的访问是一致的,即组合能让用户以一致的方式处理个别对象以及组合对象。

原理类图

类图说明

  • 1.Component是组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Conponent的子部件,Component可以是抽象类,也可以是接口。
  • 2.Leaf在组合中表示子节点,叶子节点没有子节点。
  • 3.Composite非叶子节点,用于存储子部件,在Composite接口中实现子部件的相关操作,比如增加(add),删除(remove)。

组合模式实现案例UML类图

public class OrganizationComponent {

    private String name;//组织名称

    private String des;//组织描述

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

    //方法,进行默认空实现,因为叶子节点不需要实现这些方法
    //如果做成抽象类,则叶子节点需要做无用的空实现
    public void add(OrganizationComponent o){}

    public void remove(OrganizationComponent o){}

    public void print(){}

    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;
    }
}
public class University extends OrganizationComponent {
    //组合 包含下一个节点的集合 (学院集合)
    public List<OrganizationComponent> ocs = new ArrayList<OrganizationComponent>();

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

    @Override
    public void add(OrganizationComponent o) {
        ocs.add(o);
    }

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

    @Override
    public void print() {
        System.out.println("------" + getName() + "------");
        for (OrganizationComponent o : ocs) {
            o.print();
        }
    }
}
public class College extends OrganizationComponent{

    //组合 包含下一个节点的集合 (专业集合)
    public List<OrganizationComponent> ocs = new ArrayList<OrganizationComponent>();

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

    @Override
    public void add(OrganizationComponent o) {
        ocs.add(o);
    }

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

    @Override
    public void print() {
        System.out.println("------" + getName() + "------");
        for (OrganizationComponent o : ocs) {
            o.print();
        }
    }

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

    @Override
    public void print() {
        System.out.println(this.getName());
    }
}
//测试
public class Client {
    public static void main(String[] args) {
        OrganizationComponent university = new University("清华大学", "test");

        OrganizationComponent infoProjectCollege = new College("信息工程学院", "test");
        OrganizationComponent softwareCollege = new College("软件学院", "test");

        infoProjectCollege.add(new Department("物联网工程","test"));
        infoProjectCollege.add(new Department("通信工程","test"));
        infoProjectCollege.add(new Department("电子信息","test"));

        softwareCollege.add(new Department("软件工程","test"));
        softwareCollege.add(new Department("计算机科
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Service 层异常抛到 Controller .. 下一篇Springboot中使用线程池的三种方式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目