亲宝软件园·资讯

展开

Java组合模式

爱学习的大鱼 人气:0

模式介绍

UML类图

类图解析:

组合模式案例

背景介绍:一所大学包含多个学院,学院又包含多个学科专业

OrganizationComponent(抽象类)

public abstract class OrganizationComponent {
    private String name;
    private String des;
    /**
     * 默认实现
     * @param o
     */
    protected void add(OrganizationComponent o) {
        throw new UnsupportedOperationException("该方法不支持");
    }
    protected void remove(OrganizationComponent o) {
        throw new UnsupportedOperationException("该方法不支持");
    }
    public OrganizationComponent(String name, String des) {
        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;
    }

    /**
     * 打印方法
     */
    protected abstract void print();
}

Department(专业类)

public class Department extends OrganizationComponent{
    public Department(String name, String des) {
        super(name, des);
    }
    @Override
    protected void print() {
        System.out.println(getName());
    }
}

College(学院类)

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

    public College(String name, String des) {
        super(name, des);
    }
    @Override
    protected void add(OrganizationComponent o) {
        departments.add(o);
    }
    @Override
    protected void remove(OrganizationComponent o) {
        departments.remove(o);
    }
    @Override
    protected void print() {
        System.out.println("-----------" + getName() + "-----------");
        departments.forEach(OrganizationComponent::print);
    }
}

University(大学类)

public class University extends OrganizationComponent {
    List<OrganizationComponent> colleges = new ArrayList<>();

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

    @Override
    protected void add(OrganizationComponent o) {
        colleges.add(o);
    }

    @Override
    protected void remove(OrganizationComponent o) {
        colleges.remove(o);
    }
    @Override
    protected void print() {
        System.out.println("-----------" + getName() + "-----------");
        colleges.forEach(OrganizationComponent::print);
    }
}

Client(测试类)

public class Client {
    public static void main(String[] args) {
        // 创建大学
        University university = new University("清华大学", "中国顶级大学");

        // 计算机学院
        College computerCollege = new College("计算机学院", "计算机学院");
        College 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();
    }
}

实现结果:

组合模式的注意事项和细节

加载全部内容

相关教程
猜你喜欢
用户评论