設計模式(十)——組合模式(HashMap源碼解析)


1 看一個學校院系展示需求

編寫程序展示一個學校院系結構:需求是這樣,要在一個頁面中展示出學校的院系組成,一個學校有多個學院, 一個學院有多個系。如圖:

 

2 傳統方案解決學校院系展示

3 傳統方案解決學校院系展示存在的問題分析

1) 將學院看做是學校的子類,系是學院的子類,這樣實際上是站在組織大小來進行分層次的

2) 實際上我們的要求是 :在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系,

此這種方案,不能很好實現的管理的操作,比如對學院、系的添加,刪除,遍歷等

3) 解決方案:把學校、院、系都看做是組織結構,他們之間沒有繼承的關系,而是一個樹形結構,可以更好的實現管理操作。 => 組合模式

4 組合模式基本介紹

基本介紹

1) 組合模式(Composite Pattern),又叫部分整體模式,它創建了對象組的樹形結構,將對象組合成樹狀結構以表示“整體-部分”的層次關系。

2) 組合模式依據樹形結構來組合對象,用來表示部分以及整體層次。

3) 這種類型的設計模式屬於結構型模式。

4) 組合模式使得用戶對單個對象和組合對象的訪問具有一致性,即:組合能讓客戶以一致的方式處理個別對象以及組合對象

5 組合模式原理類

 

對原理結構圖的說明-即(組合模式的角色及職責)

1) Component :這是組合中對象聲明接口,在適當情況下,實現所有類共有的接口默認行為,用於訪問和管理

Component 子部件, Component  可以是抽象類或者接口

2) Leaf : 在組合中表示葉子節點,葉子節點沒有子節點

3) Composite :非葉子節點,  用於存儲子部件  在 Component 接口中實現 子部件的相關操作,比如增加(add), 刪除。

組合模式解決學校院系展示的 應用實例

應用實例要求

1) 編寫程序展示一個學校院系結構:需求是這樣,要在一個頁面中展示出學校的院系組成,一個學校有多個學院, 一個學院有多個系。

2) 思路分析和圖解(類圖)

 

 

OrganizationComponmet 類
package com.lin.composite;


public abstract class OrganizationComponmet {

    private String name;
    
    private String des;
    
    protected void add(OrganizationComponmet organizationComponmet) {
        throw new UnsupportedOperationException();
    }
    
    protected void remove(OrganizationComponmet organizationComponmet) {
        throw new UnsupportedOperationException();
    }

    public OrganizationComponmet(String name, String des) {
        super();
        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();
    
    
}
University 類
package com.lin.composite;

import java.util.ArrayList;
import java.util.List;

public class University extends OrganizationComponmet{
    
    List<OrganizationComponmet> organizationComponmets = new ArrayList<OrganizationComponmet>();

    

    public University(String name, String des) {
        super(name, des);
    }
    
    @Override
    protected void add(OrganizationComponmet organizationComponmet) {
        organizationComponmets.add(organizationComponmet);
    }

    @Override
    protected void remove(OrganizationComponmet organizationComponmet) {
        organizationComponmets.remove(organizationComponmet);
    }
    
    
    @Override
    public String getDes() {
        return super.getDes();
    }
    
    @Override
        public String getName() {
            return super.getName();
        }
    
    
    @Override
    protected void print() {
    
        System.out.println("---------------------" + getName() + "--------------------");
        
        for (OrganizationComponmet organizationComponmet : organizationComponmets) {
            organizationComponmet.print();
        }
    }

    
}
College 類
package com.lin.composite;

import java.util.ArrayList;
import java.util.List;

public class College extends OrganizationComponmet{

List<OrganizationComponmet> organizationComponmets = new ArrayList<OrganizationComponmet>();

    

    public College(String name, String des) {
        super(name, des);
    }
    
    @Override
    protected void add(OrganizationComponmet organizationComponmet) {
        organizationComponmets.add(organizationComponmet);
    }

    @Override
    protected void remove(OrganizationComponmet organizationComponmet) {
        organizationComponmets.remove(organizationComponmet);
    }
    
    
    @Override
    public String getDes() {
        return super.getDes();
    }
    
    @Override
        public String getName() {
            return super.getName();
        }
    
    @Override
    protected void print() {
    
        System.out.println("---------------------" + getName() + "--------------------");
        
        for (OrganizationComponmet organizationComponmet : organizationComponmets) {
            organizationComponmet.print();
        }
    }

    
}
Department 類
package com.lin.composite;

public class Department extends OrganizationComponmet{

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

    @Override
    public String getDes() {
        return super.getDes();
    }
    
    @Override
        public String getName() {
            return super.getName();
        }
    
    @Override
    protected void print() {
        System.out.println(getName());
    }
    
}

Client類

package com.lin.composite;

public class Client {

    public static void main(String[] args) {
        
        OrganizationComponmet university = new University("波大", "美國大學");
        
        OrganizationComponmet college1 = new College("計算機學院", "計算機");
        OrganizationComponmet college2 = new College("中文學院", "中文");
        
        university.add(college1);
        university.add(college2);
        
        OrganizationComponmet department1 = new Department("軟件工程專業", "軟件");
        OrganizationComponmet department2 = new Department("大數據專業", "大數據");
        OrganizationComponmet department3 = new Department("漢語言專業", "漢語言");
        OrganizationComponmet department4 = new Department("中華文化專業", "中華文化");
        
        college1.add(department1);
        college1.add(department2);
        college2.add(department3);
        college2.add(department4);
        
        university.print();
        System.out.println("--------------------------------------------");
        college1.print();
    }
}

組合模式在 JDK 集合的源碼分析

1) Java 的集合類-HashMap 就使用了組合模式

2) 代碼分析

 

package com.lin.composite;

import java.util.HashMap;
import java.util.Map;

public class CompositeApply {

    public static void main(String[] args) {
        Map<Object, Object> hashMap = new HashMap<Object, Object>();
        hashMap.put(0, "zero");
        System.out.println(hashMap);
        
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        
        hashMap.putAll(map);
        System.out.println(hashMap);
    }
}

8 組合模式的注意事項和細節

1) 簡化客戶端操作。客戶端只需要面對一致的對象而不用考慮整體部分或者節點葉子的問題。

2) 具有較強的擴展性。當我們要更改組合對象時,我們只需要調整內部的層次關系,客戶端不用做出任何改動.

3) 方便創建出復雜的層次結構。客戶端不用理會組合里面的組成細節,容易添加節點或者葉子從而創建出復雜的樹形結構

4) 需要遍歷組織機構,或者處理的對象具有樹形結構時, 非常適合使用組合模式.

要求較高的抽象性,如果節點和葉子有很多差異性的話,比如很多方法和屬性都不一樣,不適合使用組合模式

 

僅供參考,有錯誤還請指出!

有什么想法,評論區留言,互相指教指教。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM