圖解設(shè)計(jì)模式--Composite(組合)模式

容器與內(nèi)容的一致性

Composite 模式

能夠使容器與內(nèi)容具有一致性,創(chuàng)造出遞歸結(jié)構(gòu)的模式。

示例程序

名字 說明 角色
Entry 抽象類,用來實(shí)現(xiàn) File 類和 Directory 類的一致性 Component
File 表示文件的類 Leaf
Directory 表示文件夾的類 Composite
FileTreatmentException 表示向文件夾增加 Entry 時(shí)發(fā)生的異常的類
Main 測(cè)試程序行為的類 Client

Entry.java

package composite;

public abstract class Entry {

    public abstract String getName();
    public abstract int getSize();
    public Entry add(Entry entry) throws FileTreatmentException {
        throw new FileTreatmentException();
    }
    public void printList() {
        printList("");
    }
    protected abstract void printList(String prefix);

    public String toString() {
        return getName() + "(" + getSize() + ")";
    }

}

Directory.java

package composite;

import java.util.ArrayList;
import java.util.Iterator;

public class Directory extends Entry{
    private String name;
    private ArrayList directory = new ArrayList();
    public Directory(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getSize() {
        int size = 0;
        Iterator it = directory.iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            size += entry.getSize();
        }
        return size;
    }
    public Entry add(Entry entry) {
        directory.add(entry);
        return this;
    }
    protected void printList(String prefix) {
        System.out.println(prefix + "/" + this);
        Iterator it = directory.iterator();
        while (it.hasNext()) {
            Entry entry = (Entry)it.next();
            entry.printList(prefix + "/" + name);
        }
    }
}

File.java

package composite;

public class File extends Entry {
    private String name;
    private int size;
    public File(String name, int size) {
        this.name = name;
        this.size = size;
    }
    public String getName() {
        return name;
    }
    public int getSize() {
        return size;
    }
    protected void printList(String prefix) {
        System.out.println(prefix + "/" + this);
    }
}

FileTreatmentException.java

package composite;

public class FileTreatmentException extends RuntimeException {
    public FileTreatmentException(){}
    public FileTreatmentException(String msg){
        super(msg);
    }
}

Main.java

package composite;

public class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Making root entries...");
            Directory rootdir = new Directory("root");
            Directory bindir = new Directory("bin");
            Directory tmpdir = new Directory("tmp");
            Directory usrdir = new Directory("usr");
            rootdir.add(bindir);
            rootdir.add(tmpdir);
            rootdir.add(usrdir);
            bindir.add(new File("vi", 10000));
            bindir.add(new File("latex", 20000));
            rootdir.printList();

            System.out.println("");
            System.out.println("Making user entries...");
            Directory yuki = new Directory("yuki");
            Directory hanako = new Directory("hanako");
            Directory tomura = new Directory("tomura");
            usrdir.add(yuki);
            usrdir.add(hanako);
            usrdir.add(tomura);
            yuki.add(new File("diary.html", 100));
            yuki.add(new File("Composite.java", 200));
            hanako.add(new File("memo.tex", 300));
            tomura.add(new File("game.doc", 400));
            tomura.add(new File("junk.mail", 500));

            rootdir.printList();
        } catch (FileTreatmentException e) {
            e.printStackTrace();
        }
    }
}

Composite 模式中的角色

  1. Leaf(樹葉)

    表示“內(nèi)容”的角色。在該角色中不能放入其他對(duì)象。

  2. Composite(組合)

    表示容器角色。可以在其中放入 Leaf 角色和 Composite 角色。

  3. Component(組件)

    使 Leaf 角色和 Composite 角色具有一致性的角色。是Leaf、Composite的父親。

  4. Client

    使用 Composite 模式的角色。

拓展思路

一、Add 方法應(yīng)該放在哪里?

  1. 定義在 Entry 類中,報(bào)錯(cuò)
  2. 定義在 Entry 類中,但什么都不做
  3. 定義在 Entry 類中,但不實(shí)現(xiàn)
  4. 只定義在 Directory 類中

二、通常來說,樹結(jié)構(gòu)的數(shù)據(jù)結(jié)構(gòu)都適用 Composite 模式。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 場(chǎng)景問題# 1.1 商品類別樹## 考慮這樣一個(gè)實(shí)際的應(yīng)用:管理商品類別樹。 在實(shí)現(xiàn)跟商品有關(guān)的應(yīng)用系統(tǒng)的時(shí)候...
    七寸知架構(gòu)閱讀 6,259評(píng)論 10 59
  • 引入composite模式 composite模式的實(shí)例 composite模式的分析 小結(jié) 引入composit...
    六尺帳篷閱讀 16,264評(píng)論 1 13
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,628評(píng)論 18 399
  • 想和你去100個(gè)城市.來99個(gè)擁抱.看98場(chǎng)日落.要97次接吻.拍96張照片.買95朵玫瑰.去94家餐館.看93次...
    onlooker納木錯(cuò)閱讀 281評(píng)論 0 1
  • 萬病之源源于血液不干凈 血液可以運(yùn)輸氧氣,營(yíng)養(yǎng),荷爾蒙,熱量,免疫球蛋白(灰指甲,腳氣,濕疹),廢物(老年斑,痤瘡)
    阿果的幸運(yùn)閱讀 151評(píng)論 0 0

友情鏈接更多精彩內(nèi)容