【結構型模式】組合模式

前言(一些廢話,可以忽略)

  • 組合模式,是我們依賴關係中的一種,在這裏又變換成爲一種設計模式,那麼它的核心肯定也是將類與類通過組合的方式拼湊在一起了
  • PS.部分類實現見文末

要解決的問題

  • 我們都知道一個單位,有組織架構,一個學校有各個不同的院系,一個院系又有不同的部門,那麼我們式樣哪種方式來方便擴展的,符合代碼設計原則的方式來組織這種層級架構關係呢
  • 這就是組合模式,通過將一個不同層級的組織機構一層一層的關聯,既方便了擴展,又十分清晰的捋順了各個方面的線條

組合模式

  • 以學校來舉例,學校下面有院系,院系下面有部門,但是這三種層級結構都是組織,所以我們創建父類或接口
  • 可以向頂級組織學校中添加院系,可以向院系中添加部門,及刪改查等
/**
 * @program: ade-someproblem
 * @author: cade franklin
 * @create: 2019-12-29 29:11
 **/
public interface OrganizationComponent {
    void add(OrganizationComponent department);
    void print();
    void remove(OrganizationComponent department);
}
  • 學校,是一個組織,實現OrganizationComponent,在它中間包含院系的List,將其組合進來
  • 另外院系和部門的組織相似

/**
 * @program: ade-someproblem
 * @author: cade franklin
 * @create: 2019-12-29 29:11
 **/
public class University implements OrganizationComponent {

    private String name;

    private List<College> collegeList = new ArrayList();

    public University(String name) {
        this.name = name;
    }

    @Override
    public void add(OrganizationComponent organizationComponent) {
        if(organizationComponent instanceof College){
            College college = (College)organizationComponent;
            collegeList.add(college);
        }
    }

    @Override
    public void print() {
        System.out.println("======University.print:"+name);

        for (int i = 0; i < collegeList.size(); i++) {
            OrganizationComponent college = collegeList.get(i);
            college.print();
        }
    }

    @Override
    public void remove(OrganizationComponent organizationComponent) {
        if(organizationComponent instanceof College){
            College college = (College)organizationComponent;
            collegeList.remove(college);
        }
    }
}
  • 我們的使用如下
/**
 * @program: ade-someproblem
 * @author: cade franklin
 * @create: 2019-12-29 29:11
 **/
public class ComponsiteMode {
    public static void main(String[] args) {

        OrganizationComponent department = new Department("團委");
        OrganizationComponent department1 = new Department("院辦");
        OrganizationComponent department2 = new Department("教研室");

        OrganizationComponent college = new College("計算機學院");
        college.add(department);
        college.add(department1);
        college.add(department2);

        OrganizationComponent college1 = new College("電子信息學院");

        OrganizationComponent university = new University("XXX大學");
        university.add(college);
        university.add(college1);

//	需要打印學校就直接從學校開始調用
//        university.print();
//	如果需要從下一層級遍歷,直接使用下一層級,非常的靈活
        college.print();

    }
}

總結

  • 組合模式是通過組合的方式,將下一層級的組織結構組合在類中,這樣能夠靈活的使用

附錄代碼

/**
 * @program: ade-someproblem
 * @author: cade franklin
 * @create: 2019-12-29 29:11
 **/
public class Department implements OrganizationComponent {

    private String name;

    public Department(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public void add(OrganizationComponent department) {
        throw new UnsupportedOperationException("Department.add");
    }

    @Override
    public void print() {
        System.out.println("Department.print:"+name);
    }

    @Override
    public void remove(OrganizationComponent department) {
        throw new UnsupportedOperationException("Department.remove");
    }

}
/**
 * @program: ade-someproblem
 * @author: cade franklin
 * @create: 2019-12-29 29:11
 **/
public class College implements OrganizationComponent {

    private String name;

    private List<Department> departmentList = new ArrayList();

    public College(String name) {
        this.name = name;
    }

    @Override
    public void add(OrganizationComponent organizationComponent) {
        if(organizationComponent instanceof Department){
            Department department = (Department)organizationComponent;
            departmentList.add(department);
        }

    }

    @Override
    public void print() {
        System.out.println("------College.print:"+name);
        for (int i = 0; i < departmentList.size(); i++) {
            OrganizationComponent department = departmentList.get(i);
            department.print();
        }
    }

    @Override
    public void remove(OrganizationComponent organizationComponent) {
        if(organizationComponent instanceof Department){
            Department department = (Department)organizationComponent;
            departmentList.remove(department);
        }
    }
}

願你不捨愛與自由。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章