设计模式——组合模式

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

比如例子:

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

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

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

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

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();
    }
}
上一篇:微信小程序:实现左右滑动的日历组件效果


下一篇:MTTF、MTBF与MTRF