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

當(dāng)前位置:首頁 > 科技  > 軟件

一文徹底搞明白組合模式

來源: 責(zé)編: 時(shí)間:2024-05-09 09:20:49 131觀看
導(dǎo)讀本篇講解Java設(shè)計(jì)模式中的組合模式,分為定義、模式應(yīng)用前案例、結(jié)構(gòu)、模式應(yīng)用后案例、適用場景、模式可能存在的困惑和本質(zhì)探討7個(gè)部分。定義組合模式是將對象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式

本篇講解Java設(shè)計(jì)模式中的組合模式,分為定義、模式應(yīng)用前案例、結(jié)構(gòu)、模式應(yīng)用后案例、適用場景、模式可能存在的困惑和本質(zhì)探討7個(gè)部分。PID28資訊網(wǎng)——每日最新資訊28at.com

定義

組合模式是將對象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式使得客戶對單個(gè)對象和復(fù)合對象的使用具有一致性。PID28資訊網(wǎng)——每日最新資訊28at.com

在新的分類方式中,組合模式被劃分至類之間的交互類別中,其簡化的是調(diào)用方與具備樹結(jié)構(gòu)的一組對象之間的交互,具體通過一致性的行為實(shí)現(xiàn)PID28資訊網(wǎng)——每日最新資訊28at.com

PID28資訊網(wǎng)——每日最新資訊28at.com

模式應(yīng)用前案例

下面以一個(gè)典型的文件和目錄為例來進(jìn)行說明,先來看一下未應(yīng)用組合模式之前的代碼實(shí)現(xiàn)。PID28資訊網(wǎng)——每日最新資訊28at.com

public class File {//文件結(jié)構(gòu)    private final String name;    public File(String name) {        this.name = name;    }    public void display() {        System.out.println("File: " + this.name);    }}public class Directory {//目錄結(jié)構(gòu)    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<>();    }    // 添加子節(jié)點(diǎn)    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 {//調(diào)用方代碼    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();    }}

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

結(jié)構(gòu)

PID28資訊網(wǎng)——每日最新資訊28at.com

組合模式的示例代碼如下。PID28資訊網(wǎng)——每日最新資訊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) {        // 創(chuàng)建葉子節(jié)點(diǎn)        Component leaf1 = new Leaf("LeafA");        Component leaf2 = new Leaf("LeafB");        Component leaf3 = new Leaf("LeafC");        // 創(chuàng)建復(fù)合節(jié)點(diǎn)        Component composite = new Composite("CompositeX");        composite.add(leaf1);        composite.add(leaf2);        // 創(chuàng)建另一個(gè)復(fù)合節(jié)點(diǎn),并添加之前的復(fù)合節(jié)點(diǎn)和新的葉子節(jié)點(diǎn)        Component root = new Composite("Root");        root.add(composite);        root.add(leaf3);        // 執(zhí)行操作        root.operation();    }}

模式應(yīng)用后案例

上面文件與目錄的案例,使用組合模式之后的代碼實(shí)現(xiàn)如下。PID28資訊網(wǎng)——每日最新資訊28at.com

public interface IComponent {//接口    void display();}public class File implements IComponent{//文件實(shí)現(xiàn)    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{//目錄實(shí)現(xiàn)    private String name;    private final List<IComponent> children;    // 初始化方法    public Directory(String name){        this.name = this.name;        this.children = new ArrayList<>();    }    // 添加子節(jié)點(diǎn)    public void addComponent(IComponent component){        this.children.add(component);    }    // 顯示目錄內(nèi)容    @Override    public void display() {       //System.out.println("Directory: " + this.name);        for (IComponent child : this.children) {            child.display();        }    }}public class Client {//調(diào)用方代碼    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();    }}

在上述代碼中,由于樹的結(jié)構(gòu)使用一個(gè)接口和實(shí)現(xiàn)的家族來實(shí)現(xiàn),這樣樹的結(jié)構(gòu)中所有類的行為都是一致的,簡化了編碼時(shí)的復(fù)雜度。PID28資訊網(wǎng)——每日最新資訊28at.com

適用場景

當(dāng)需求中出現(xiàn)的一系列概念或?qū)ο螅鼈冎g存在部分-整體的層次結(jié)構(gòu)或共同構(gòu)成一顆樹的結(jié)構(gòu)時(shí),就可以考慮使用組合模式。PID28資訊網(wǎng)——每日最新資訊28at.com

模式可能存在的困惑

困惑1:組合模式中的“組合”,與“組合優(yōu)于繼承”中的“組合”,有什么關(guān)聯(lián)?PID28資訊網(wǎng)——每日最新資訊28at.com

兩者都代表了一種關(guān)系。前者的“組合”指的是將一系列對象按照層次化結(jié)構(gòu)進(jìn)行組織。而后者的“組合”指的是兩個(gè)對象之間的聚合或組合關(guān)系,以此來取代類之間繼承關(guān)系。PID28資訊網(wǎng)——每日最新資訊28at.com

本質(zhì)

組合模式的本質(zhì)在于提供了一種機(jī)制來處理對象之間的部分-整體關(guān)系,并且通過統(tǒng)一接口來簡化調(diào)用方使用復(fù)雜層次結(jié)構(gòu)時(shí)可能遇到的問題。PID28資訊網(wǎng)——每日最新資訊28at.com

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

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 大營銷抽獎系統(tǒng),DDD開發(fā)要如何建模?

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

標(biāo)簽:
  • 熱門焦點(diǎn)
  • 一加Ace2 Pro真機(jī)揭曉 鈦空灰配色質(zhì)感拉滿

    終于,在經(jīng)過了幾波預(yù)熱之后,一加Ace2 Pro的外觀真機(jī)圖在網(wǎng)上出現(xiàn)了。還是博主數(shù)碼閑聊站曝光的,這次的外觀設(shè)計(jì)還是延續(xù)了一加11的方案,只是細(xì)節(jié)上有了調(diào)整,例如新加入了鈦空灰
  • 5月安卓手機(jī)好評榜:魅族20 Pro奪冠

    性能榜和性價(jià)比榜之后,我們來看最后的安卓手機(jī)好評榜,數(shù)據(jù)來源安兔兔評測,收集時(shí)間2023年5月1日至5月31日,僅限國內(nèi)市場。第一名:魅族20 Pro好評率:97.50%不得不感慨魅族老品牌還
  • 容量越大越不壞?24萬塊硬盤故障率報(bào)告公布 這些產(chǎn)品零故障

    8月5日消息,云存儲服務(wù)商Backblaze發(fā)布了最新的硬盤故障率報(bào)告,年故障率有所上升。Backblaze發(fā)布的硬盤季度統(tǒng)計(jì)數(shù)據(jù),其中包括故障率等重要方面。這些結(jié)
  • 一文看懂為蘋果Vision Pro開發(fā)應(yīng)用程序

    譯者 | 布加迪審校 | 重樓蘋果的Vision Pro是一款混合現(xiàn)實(shí)(MR)頭戴設(shè)備。Vision Pro結(jié)合了虛擬現(xiàn)實(shí)(VR)和增強(qiáng)現(xiàn)實(shí)(AR)的沉浸感。其高分辨率顯示屏、先進(jìn)的傳感器和強(qiáng)大的處理能力
  • 一個(gè)注解實(shí)現(xiàn)接口冪等,這樣才優(yōu)雅!

    場景碼猿慢病云管理系統(tǒng)中其實(shí)高并發(fā)的場景不是很多,沒有必要每個(gè)接口都去考慮并發(fā)高的場景,比如添加住院患者的這個(gè)接口,具體的業(yè)務(wù)代碼就不貼了,業(yè)務(wù)偽代碼如下:圖片上述代碼有
  • 雅柏威士忌多款單品價(jià)格大跌,泥煤頂流也不香了?

    來源 | 烈酒商業(yè)觀察編 | 肖海林今年以來,威士忌市場開始出現(xiàn)了降溫跡象,越來越多不斷暴漲的網(wǎng)紅威士忌也開始悄然回歸市場理性。近日,LVMH集團(tuán)旗下蘇格蘭威士忌品牌雅柏(Ardbeg
  • 共享單車的故事講到哪了?

    來源丨海克財(cái)經(jīng)與共享充電寶相差不多,共享單車已很久沒有被國內(nèi)熱點(diǎn)新聞關(guān)照到了。除了一再漲價(jià)和用戶直呼用不起了。近日多家媒體再發(fā)報(bào)道稱,成都、天津、鄭州等地多個(gè)共享單
  • 騰訊VS網(wǎng)易,最卷游戲暑期檔,誰能笑到最后?

    作者:無銹缽來源:財(cái)經(jīng)無忌7月16日晚,上海1862時(shí)尚藝術(shù)中心。伴隨著幻象的精準(zhǔn)命中,碩大的熒幕之上,比分被定格在了14:12,被寄予厚望的EDG戰(zhàn)隊(duì)以絕對的優(yōu)勢戰(zhàn)勝了BLG戰(zhàn)隊(duì),拿下了總決
  • 半導(dǎo)體需求下滑 三星電子DS業(yè)務(wù)部門今年?duì)I業(yè)虧損預(yù)計(jì)超10萬億韓元

    7月17日消息,據(jù)外媒報(bào)道,去年下半年開始的半導(dǎo)體需求下滑,影響到了三星電子、SK海力士、英特爾等諸多廠商,營收明顯下滑,部分廠商甚至出現(xiàn)了虧損。作為
Top