AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

一文徹底搞明白組合模式

來源: 責編: 時間:2024-05-09 09:20:49 148觀看
導讀本篇講解Java設計模式中的組合模式,分為定義、模式應用前案例、結構、模式應用后案例、適用場景、模式可能存在的困惑和本質探討7個部分。定義組合模式是將對象組合成樹形結構以表示“部分-整體”的層次結構。組合模式

本篇講解Java設計模式中的組合模式,分為定義、模式應用前案例、結構、模式應用后案例、適用場景、模式可能存在的困惑和本質探討7個部分。v3828資訊網——每日最新資訊28at.com

定義

組合模式是將對象組合成樹形結構以表示“部分-整體”的層次結構。組合模式使得客戶對單個對象和復合對象的使用具有一致性。v3828資訊網——每日最新資訊28at.com

在新的分類方式中,組合模式被劃分至類之間的交互類別中,其簡化的是調用方與具備樹結構的一組對象之間的交互,具體通過一致性的行為實現v3828資訊網——每日最新資訊28at.com

v3828資訊網——每日最新資訊28at.com

模式應用前案例

下面以一個典型的文件和目錄為例來進行說明,先來看一下未應用組合模式之前的代碼實現。v3828資訊網——每日最新資訊28at.com

public class File {//文件結構    private final String name;    public File(String name) {        this.name = name;    }    public void display() {        System.out.println("File: " + this.name);    }}public class Directory {//目錄結構    private String name;    private final List<File> files;    private final List<Directory> directories;    // 初始化方法    public Directory(String name){        this.name = this.name;        this.files = new ArrayList<>();        this.directories = new ArrayList<>();    }    // 添加子節點    public void addFile(File file){        this.files.add(file);    }    // 添加子目錄    public void addDirectory(Directory directory) {        this.directories.add(directory);    }    public void display(){        //System.out.println("Directory:"+this.name);        for(File file : this.files){            file.display();        }        for (Directory dir : this.directories) {            dir.display();        }    }}public class Client {//調用方代碼    public static void main(String[] ars){        Directory root= new Directory("Root");        File file1=new File("file1.txt");        File file2=new File("file2.txt");        root.addFile(file1);        root.addFile(file2);        Directory subDirecory =new Directory ("Subdirectory");        File file3 = new File("file3.tx");        File file4 = new File("file4.tx");        subDirecory.addFile(file3);        subDirecory.addFile(file4);        root.addDirectory(subDirecory);        root.display();    }}

我們知道,文件和目錄兩者是一個大的樹結構中的節點。在上面未使用組合模式的代碼中,文件和目錄都有自己定義的方法。這樣在構建一個多層樹結構的過程中,復雜度會提升。v3828資訊網——每日最新資訊28at.com

結構

v3828資訊網——每日最新資訊28at.com

組合模式的示例代碼如下。v3828資訊網——每日最新資訊28at.com

public interface Component {    void operation();    void add(Component component);    void remove(Component component);    Component Display(int index);}public class Leaf implements Component{    private String name;    public Leaf(String name) {        this.name = name;    }    @Override    public void operation() {        System.out.println("Leaf: " + name + " operation()");    }    @Override    public void add(Component component) {        throw new UnsupportedOperationException("Leaf cannot have children");    }    @Override    public void remove(Component component) {        throw new UnsupportedOperationException("Leaf cannot remove children");    }    @Override    public Component Display(int index) {        throw new UnsupportedOperationException("Leaf cannot get child");    }}public interface Component {    void operation();    void add(Component component);    void remove(Component component);    Component Display(int index);}public class Client {    public static void main(String[] args) {        // 創建葉子節點        Component leaf1 = new Leaf("LeafA");        Component leaf2 = new Leaf("LeafB");        Component leaf3 = new Leaf("LeafC");        // 創建復合節點        Component composite = new Composite("CompositeX");        composite.add(leaf1);        composite.add(leaf2);        // 創建另一個復合節點,并添加之前的復合節點和新的葉子節點        Component root = new Composite("Root");        root.add(composite);        root.add(leaf3);        // 執行操作        root.operation();    }}

模式應用后案例

上面文件與目錄的案例,使用組合模式之后的代碼實現如下。v3828資訊網——每日最新資訊28at.com

public interface IComponent {//接口    void display();}public class File implements IComponent{//文件實現    private final String name;    public File(String name) {        this.name = name;    }    @Override    public void display() {        System.out.println("File: " + this.name);    }}public class Directory implements IComponent{//目錄實現    private String name;    private final List<IComponent> children;    // 初始化方法    public Directory(String name){        this.name = this.name;        this.children = new ArrayList<>();    }    // 添加子節點    public void addComponent(IComponent component){        this.children.add(component);    }    // 顯示目錄內容    @Override    public void display() {       //System.out.println("Directory: " + this.name);        for (IComponent child : this.children) {            child.display();        }    }}public class Client {//調用方代碼    public static void main(String[] ars){        Directory root= new Directory("Root");        File file1 = new File("file1.txt");        File file2 = new File ("file2.txt");        root.addComponent(file1);        root.addComponent(file2);        Directory subDirectory =new Directory ("Subdirectory");        File file3 = new File("file3.txt");        File file4 = new File("file4.txt");        subDirectory.addComponent(file3);        subDirectory.addComponent(file4);        root.addComponent(subDirectory);        root.display();    }}

在上述代碼中,由于樹的結構使用一個接口和實現的家族來實現,這樣樹的結構中所有類的行為都是一致的,簡化了編碼時的復雜度。v3828資訊網——每日最新資訊28at.com

適用場景

當需求中出現的一系列概念或對象,它們之間存在部分-整體的層次結構或共同構成一顆樹的結構時,就可以考慮使用組合模式。v3828資訊網——每日最新資訊28at.com

模式可能存在的困惑

困惑1:組合模式中的“組合”,與“組合優于繼承”中的“組合”,有什么關聯?v3828資訊網——每日最新資訊28at.com

兩者都代表了一種關系。前者的“組合”指的是將一系列對象按照層次化結構進行組織。而后者的“組合”指的是兩個對象之間的聚合或組合關系,以此來取代類之間繼承關系。v3828資訊網——每日最新資訊28at.com

本質

組合模式的本質在于提供了一種機制來處理對象之間的部分-整體關系,并且通過統一接口來簡化調用方使用復雜層次結構時可能遇到的問題。v3828資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-87479-0.html一文徹底搞明白組合模式

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 大營銷抽獎系統,DDD開發要如何建模?

下一篇: 開發者對 React 19 Beta 發布感到困惑

標簽:
  • 熱門焦點
  • 共享單車的故事講到哪了?

    來源丨海克財經與共享充電寶相差不多,共享單車已很久沒有被國內熱點新聞關照到了。除了一再漲價和用戶直呼用不起了。近日多家媒體再發報道稱,成都、天津、鄭州等地多個共享單
  • 本地生活這塊肥肉,拼多多也想吃一口

    出品/壹覽商業 作者/李彥編輯/木魚拼多多也看上本地生活這塊蛋糕了。近期,拼多多在App首頁&ldquo;充值中心&rdquo;入口上線了本機生活界面。壹覽商業發現,該界面目前主要
  • 消費結構調整丨巨頭低價博弈,拼多多還卷得動嗎?

    來源:征探財經作者:陳香羽隨著流量紅利的退潮,電商的存量博弈越來越明顯。曾經主攻中高端與品質的淘寶天貓、京東重拾&ldquo;低價&rdquo;口號。而過去與他們錯位競爭的拼多多,靠
  • 品牌洞察丨服務本地,美團直播成效幾何?

    來源:17PR7月11日,美團App首頁推薦位出現&ldquo;美團直播&rdquo;的固定入口。在直播聚合頁面,外賣&ldquo;神槍手&rdquo;直播間、美團旅行直播間、美團買菜直播間等均已上線,同時
  • 小米公益基金會捐贈2500萬元馳援北京、河北暴雨救災

    8月2日消息,今日小米科技創始人雷軍在其微博上發布消息稱,小米公益基金會宣布捐贈2500萬元馳援北京、河北暴雨救災。攜手抗災,京冀安康!以下為公告原文
  • 7月4日見!iQOO 11S官宣:“雞血版”驍龍8 Gen2+200W快充加持

    上半年已接近尾聲,截至目前各大品牌旗下的頂級旗艦都已悉數亮相,而下半年即將推出的頂級旗艦已經成為了數碼圈爆料的主流,其中就包括全新的iQOO 11S系
  • iQOO Neo8 Pro評測:旗艦雙芯加持 最強性能游戲旗艦

    【Techweb評測】去年10月,iQOO推出了一款Neo7手機,該機搭載了聯發科天璣9000+,配備獨顯芯片Pro+,帶來了同價位段最佳的游戲體驗,一經上市便受到了諸多用
  • 電博會與軟博會實現"線下+云端"的雙線融合

    在本次“電博會”與“軟博會”雙展會利好條件的加持下,既可以發揮展會拉動人流、信息流、資金流實現快速交互流動的作用,繼而推動區域經濟良性發展;又可以聚
  • 北京:科技教育體驗基地開始登記

      北京“科技館之城”科技教育體驗基地登記和認證工作日前啟動。首批北京科技教育體驗基地擬于2023年全國科普日期間掛牌,后續還將開展常態化登記?! ”本┛萍冀逃w驗基
Top