Java設計模式應用——組合模式

來源:互聯網
上載者:User

標籤:boolean   static   樹形結構   length   inf   learn   進一步   arraylist   代碼   

組合模式實際上是一種樹形資料結構。以windows目錄系統舉例,怎麼樣用java語言描述一個檔案夾?

定義一個檔案夾類,檔案夾類中包含若干個子檔案類和若干個檔案類。

進一步抽象,把檔案夾和檔案都看做節點,於是一個檔案夾就可以描述為一個節點類,包含若干個子節點。

我們看看組合模式的代碼

// 抽象節點public abstract class Node {    protected String name;    abstract void add(Node node);    abstract void removed(Node node);    abstract void foreach();    public String getName()     {        return name;    }    public void setName(String name)     {        this.name = name;    }}// 檔案類public class File extends Node{    @Override    void add(Node node)     {    }    @Override    void removed(Node node)     {    }    @Override    void foreach()     {        System.out.println("File name is " + name);    }        public File(String name)    {        this.name = name;    }}// 檔案夾類import java.util.ArrayList;import java.util.List;public class Folder extends Node{    List<Node> nodes = new ArrayList<Node>();        @Override    void add(Node node)     {        nodes.add(node);    }    @Override    void removed(Node node)     {        nodes.remove(node);    }    @Override    void foreach()     {        System.out.println("Folder name is " + name);        for(Node node : nodes)        {            node.foreach();        }    }        public Folder(String name)    {        this.name = name;    }}

 測試類別

package com.coshaho.learn.component;public class Test {    public static void main(String[] args)    {        Folder root = new Folder("Root");        Folder music = new Folder("Music");        Folder software = new Folder("Software");        root.add(music);        root.add(software);        File explain = new File("explain.txt");        File moon = new File("moon.mp3");        File word = new File("word.exe");        root.add(explain);        music.add(moon);        software.add(word);        root.foreach();    }}

在實際應用中,我們希望使用json傳遞樹形結構資料,我們希望每層節點定義都一樣,不通過類來區分葉子節點和非葉子節點。這個時候我們可以簡單的在Node對象中增加一個isLeaf屬性,用來表示該節點是否是葉子節點。這種資料結構是一種遞迴結構,可以使用遞迴演算法解析。

package com.coshaho.learn.component;import java.util.ArrayList;import java.util.List;import org.springframework.util.CollectionUtils;public class TreeNode {    private String name;        private boolean isLeaf;        private List<TreeNode> nodes = new ArrayList<TreeNode>();        private TreeNode(String name, boolean isLeaf)    {        this.name = name;        this.isLeaf = isLeaf;    }        void add(TreeNode node)     {        nodes.add(node);    }    void removed(TreeNode node)     {        nodes.remove(node);    }        public String toString()    {        String nameInfo = "name: " + name;        String typeInfo = "isLeaf: " + isLeaf;        String value ="";        if(isLeaf || CollectionUtils.isEmpty(nodes))        {            value = "{" + nameInfo + "," + typeInfo + "}";        }        else        {            String childrenInfo = "children: [";            for(TreeNode node : nodes)            {                childrenInfo = childrenInfo + node.toString() + ",";            }            childrenInfo = childrenInfo.substring(0, childrenInfo.length() - 1) + "]";            value = "{" + nameInfo + "," + typeInfo + "," + childrenInfo + "}";        }        return value;    }        public static void main(String[] args)    {        TreeNode all = new TreeNode("All", false);        TreeNode guangdong = new TreeNode("GuangdDong", true);        TreeNode sichuan = new TreeNode("SiChuan", false);        TreeNode chengdu = new TreeNode("ChengDu", true);        all.add(guangdong);        all.add(sichuan);        sichuan.add(chengdu);        System.out.println(all);    }}

 

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.