ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计模式——组合模式

2021-09-30 23:01:33  阅读:110  来源: 互联网

标签:设计模式 String 组合 des 模式 add organizationComponent OrganizationComponent name


组合模式:本质就是把不该是继承关系的类,改成树形结构,有树根节点,树中间节点和叶节点。

比如例子:

关系 学校 -> 学院 -> 专业

他们之间并非是继承关系,而是包含关系,或者说是组合关系。

随时有可能移除学院、增加学院、移除专业、增加专业;所以继承就不够灵活。

先创建一个共同的父类(此处接口也可以,甚至更好)

public abstract class OrganizationComponent  {
    private String name;//名字
    private String des;//说明

    //构造器
    public OrganizationComponent(String name, String des) {
        this.name = name;
        this.des = des;
    }

    protected void add(OrganizationComponent organizationComponent){
        //默认实现
        new UnsupportedOperationException();
    }

    protected  void remove(OrganizationComponent organizationComponent){
        //默认实现
        new UnsupportedOperationException();
    }

    //方法print,做成抽象的,子类都需要实现
    protected abstract  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 {
    List<OrganizationComponent> organizationComponentLists = new ArrayList<>();

    //构造器
    public University(String name, String des) {
        super(name, des);
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        super.add(organizationComponent);
        organizationComponentLists.add(organizationComponent);

    }

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

    @Override
    protected void print() {
        System.out.println("--------------" + getName() + "--------------");
          //遍历 organizationComponentLists
        for (OrganizationComponent organizationComponent : organizationComponentLists) {
            organizationComponent.print(); }
    }
}

创建学院类

public class College extends OrganizationComponent {
    List<OrganizationComponent> organizationComponentLists = new ArrayList<>();

    //构造器
    public College(String name, String des) {
        super(name, des);
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        super.add(organizationComponent);
        organizationComponentLists.add(organizationComponent);

    }

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

    @Override
    protected void print() {
        System.out.println("--------------" + getName() + "--------------");
          //遍历 organizationComponentLists
        for (OrganizationComponent organizationComponent : organizationComponentLists) {
            organizationComponent.print(); }
    }
}

创建专业类

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

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

用法:

    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();
          computerCollege.print();
    }
}

标签:设计模式,String,组合,des,模式,add,organizationComponent,OrganizationComponent,name
来源: https://www.cnblogs.com/gradyblog/p/15358367.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有