組合模式-Composite(Java實現)

來源:互聯網
上載者:User

標籤:parent   class   stringbu   prefix   builder   turn   file類   大小   檔案夾   

組合模式-Composite

Composite模式使得使用者對單個對象和組合對象的使用具有一致性.

以<<圖解設計模式>>的檔案例子來說: 檔案夾是檔案的組合, 檔案是單個對象, 檔案夾是多個檔案的組合. 不過對使用者來說, 無論是檔案還是檔案夾, 他希望使用一個統一的方法來管理他們.這就需要將他們再次抽象出來.

解讀一下這個類圖就明白了:

1. 檔案是一個組件

2. 檔案夾也是一個組件

3. 檔案夾裡有很多組件:

       3.1 檔案夾裡可以有檔案

       3.2 檔案夾裡可以有檔案夾

Component抽象類別

這是檔案 檔案夾的抽象定義類--組件類

/** * 這裡是檔案/檔案夾 的統一抽象類別 "組件類" */public abstract class Component {    /**     * parent是父組件的指標, 也就是本例子中的"父級目錄"     */    protected Component parent;    /**     * 擷取組件名字     */    public abstract String getName();    /**     * 擷取組件的大小     */    public abstract int getSize();    /**     * @implNote 向一個組件中添加一個組件.     * @implSpec 組合模式中的葉子(Leaf)節點不可以使用這個方法, 用例子說明的話:     * 一個檔案不可以添加一個檔案或檔案夾, 只能是檔案夾來添加檔案或檔案夾     */    public Component add(Component entry) {        throw new RuntimeException("不支援此類操作...");    }    public void printList() {        printList("");    }    protected abstract void printList(String prefix);    /**     * 擷取絕對路徑     */    public String getFullName() {        StringBuilder fullName = new StringBuilder();        Component entry = this;        do {            fullName.insert(0, "/" + entry.getName());            entry = entry.parent;        } while (entry != null);        return fullName.toString();    }    /**     * 其中的getSize() 會被子類重寫, 因為檔案大小可以很容易知道, 但是檔案夾大小需要遞迴計算     */    @Override    public String toString() {        return getName() + " (" + getSize() + ")";    }}
MyFile類

MyFile類在組合模式中是Leaf(葉子)節點, 因為他不會包含其他組件.

我們可以把組件模式想象成一顆樹(綠色表示檔案夾, 橙色表示檔案):

組合模式的葉子節點指的是檔案節點, 因為檔案永遠也不會有子節點.

而綠的c1節點雖然現在是葉子節點, 但是如果給c1檔案夾添加了一個檔案的話, 他就不再是葉子節點了.

組合模式裡無法繼續添加子節點的就被稱作Leaf(葉子)

 

上面那個樹, 等價於:

 

public class MyFile extends Component {    private String name;    private int size;    public MyFile(String name, int size) {        this.name = name;        this.size = size;    }    @Override    public String getName() {        return this.name;    }    @Override    public int getSize() {        return this.size;    }    @Override    protected void printList(String prefix) {        System.out.println(prefix + "/" + this.toString());    }}
MyDirectory類

MyDirectory類作為一個檔案夾, 可以包含其他組件(檔案/檔案夾)

public class MyDirectory extends Component {    private String name;    private ArrayList<Component> substance = new ArrayList<>();    public MyDirectory(String name) {        this.name = name;    }    @Override    public String getName() {        return this.name;    }    /**     * 擷取檔案夾的大小:     * 1. size一開始是0     * 2. 如果是檔案, 那麼直接把檔案的大小加到size上     * 3. 如果是檔案夾, 那麼就遞迴地查出該檔案夾的大小, 然後加到size上.     */    @Override    public int getSize() {        int size = 0;        for (Component entry : substance) {            size += entry.getSize();        }        return size;    }    /**     * 像一個檔案夾中添加一個檔案或檔案夾     */    @Override    public Component add(Component entry) {        substance.add(entry);        entry.parent = this;        return this;    }    /**     * 1. 列印目前的目錄的路徑     * 2. 然後列印出子目錄, 子檔案的路徑     * 3. 子目錄再列印子子目錄/子子檔案的路徑...如此遞迴     */    @Override    protected void printList(String prefix) {        System.out.println(prefix + "/" + this.toString());        for (Component entry : substance) {            entry.printList(prefix + "/" + name);        }    }}

  

Main

用於測試回合

public class Main {    public static void main(String[] args) {        /* 本例子目錄結構如下:          一級目錄   二級目錄   三級目錄            a1            |-------b1            |-------b2            |            a2            |-------c1                    |-------Composite.java                    |-------king.python            |-------c2         */        MyDirectory a1 = new MyDirectory("a1");        MyDirectory a2 = new MyDirectory("a2");        MyDirectory b1 = new MyDirectory("b1");        MyDirectory b2 = new MyDirectory("b2");        a1.add(b1).add(b2);        MyDirectory c1 = new MyDirectory("c1");        MyDirectory c2 = new MyDirectory("c2");        a2.add(c1).add(c2);        MyFile java = new MyFile("Composite.java", 100);        MyFile python = new MyFile("king.python", 214);        c1.add(java).add(python);        /*-****** getFullName() ************-*/        System.out.println(java.getFullName());        System.out.println(python.getFullName());        System.out.println("");        /*-****** getFullName() ************-*/        a1.printList();        System.out.println("-----");        a2.printList();    }}

 

組合模式-Composite(Java實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.