Java design pattern---combination pattern

Examples of combined mode application: We develop a requirement that requires the presentation of a school + college + professional scene, which is convenient for school management colleges and college management majors.
Solution: The traditional thinking is to write a school class, then write a college class to inherit the school, and then write a professional class to inherit the college. Disadvantages: This structure is not suitable for management at all levels.
Combination mode: In the way of aggregation, write a combined abstract class or interface, and then schools and colleges inherit or implement this class.


The usage scenarios of the combined mode:
1. The organization that needs to be traversed or the object to be processed has a tree structure.
2. If there are many differences between nodes and leaves, and the attributes and methods are different, it is not suitable to use this mode.


Specific examples of the combined mode:
Map interface, abstractMap, hashMap in JDK The node method is equivalent to the leaf node.
Not much nonsense, just go to the code:

Create a composite abstract class or interface

public abstract class OrganizationComponent {
    private String name;
    private String des;

    protected void add(OrganizationComponent organizationComponent){
        throw new UnsupportedOperationException();
    }

    protected void remove(OrganizationComponent organizationComponent){
        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();
}

Create a school class:


//可以管理学校
public class University extends OrganizationComponent{

    //存放的是College
    List<OrganizationComponent> organizationComponentlist = new ArrayList<OrganizationComponent>();

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

    @Override
    protected void print() {
        System.out.println("----------------"+getName()+"-------------");
        for (OrganizationComponent orl: organizationComponentlist) {
            orl.print();
        }
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponentlist.add(organizationComponent);
    }

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

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

    @Override
    public void setName(String name) {
        super.setName(name);
    }

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

    @Override
    public void setDes(String des) {
        super.setDes(des);
    }
}

Create a college class:

public class College extends OrganizationComponent{

    //存放的是department
    List<OrganizationComponent> organizationComponentlist = new ArrayList<OrganizationComponent>();

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

    @Override
    protected void print() {
        System.out.println("----------------"+getName()+"-------------");
        for (OrganizationComponent orl: organizationComponentlist) {
            orl.print();
        }
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponentlist.add(organizationComponent);
    }

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

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

    @Override
    public void setName(String name) {
        super.setName(name);
    }

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

    @Override
    public void setDes(String des) {
        super.setDes(des);
    }
}

Create the final professional department class (the last node has no management method):

public class Department extends OrganizationComponent{

    //因为是叶子节点,所以add 和remove方法就不需要了

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

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

    }

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

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

The test classes are as follows:

public class Client {
    public static void main(String[] args) {

        OrganizationComponent university = new University("麻省理工","美国一流大学");
        OrganizationComponent computerCollege = new College("计算机学院","学校里最好的学院");
        OrganizationComponent businessCollege = new College("工商管理学院","最有钱的学院");

        //创建各个学院下面的系
        computerCollege.add(new Department("软件工程","学习软件"));
        computerCollege.add(new Department("网络工程","学习网络"));
        computerCollege.add(new Department("计算机科学与技术","学习计算机原理"));

        businessCollege.add(new Department("工商管理","学习工商管理"));
        businessCollege.add(new Department("会计","学习会计"));
        businessCollege.add(new Department("工商英语","学习工商英语"));

        //将两个学院加入到学校中
        university.add(computerCollege);
        university.add(businessCollege);

        university.print();
//        computerCollege.print();
    }
}

The final output is as follows, very clear and comfortable!

----------------麻省理工-------------
----------------计算机学院-------------
软件工程
网络工程
计算机科学与技术
----------------工商管理学院-------------
工商管理
会计
工商英语

 

Guess you like

Origin blog.csdn.net/LB_Captain/article/details/114528657