1. 组合模式介绍

  在解决组织结构这种具有层级关系的结构中,如果使用传统的继承,不能很好的实现管理的操作,比如学院,系的添加,删除,遍历等;

  所有可以使用组合模式把它们都看成组织结构,没有继承关系,而是一个树形结构。

 

  2. 实现

public abstract class OrgComponent {

    private String name;

    private String desc;

    public OrgComponent(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    //默认实现,子类不需要都实现
    protected void add(OrgComponent orgComponent){
        throw new UnsupportedOperationException();
    }

    protected void remove(OrgComponent orgComponent){
        throw new UnsupportedOperationException();
    }

    //抽象方法,子类都要实现
    protected abstract void print();
}
public class University extends OrgComponent {

    List<OrgComponent> list = new ArrayList<>();

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


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

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

    @Override
    protected void add(OrgComponent orgComponent) {
        list.add(orgComponent);
    }

    @Override
    protected void remove(OrgComponent orgComponent) {
        list.remove(orgComponent);
    }

    @Override
    protected void print() {
        System.out.println("----" + getName() + "----");
        for (OrgComponent orgComponent : list){
            orgComponent.print();
        }
    }
}
public class College extends OrgComponent {

    List<OrgComponent> list = new ArrayList<>();

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

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

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

    @Override
    protected void add(OrgComponent orgComponent) {
        list.add(orgComponent);
    }

    @Override
    protected void remove(OrgComponent orgComponent) {
        list.remove(orgComponent);
    }

    @Override
    protected void print() {
        System.out.println("----" + getName() + "----");
        for (OrgComponent orgComponent : list){
            orgComponent.print();
        }
    }
}
public class Department extends OrgComponent {

    public Department(String name, String desc) {
        super(name, desc);
    }

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

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

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

    public static void main(String[] args) {
        University university = new University("清华大学", "北京");

        College computerCollege = new College("计算机学院","计算机");
        College infoCollege = new College("信息工程学院", "信息");

        computerCollege.add(new Department("软件工程","软件"));
        computerCollege.add(new Department("网络工程","网络"));

        infoCollege.add(new Department("通信工程","通信"));
        infoCollege.add(new Department("信息工程","信息"));

        university.add(computerCollege);
        university.add(infoCollege);

        university.print();
    }
}

   

  3. 组合模式在JDK集合的源码分析

  (1)hashMap就使用了组合模式,内部类Node就相当于叶子节点

 

posted on 2024-04-21 15:21  homle  阅读(11)  评论(0编辑  收藏  举报