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

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

一文徹底搞明白組合模式

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

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

定義

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

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

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

模式應用前案例

下面以一個典型的文件和目錄為例來進行說明,先來看一下未應用組合模式之前的代碼實現(xiàn)。KkN28資訊網(wǎng)——每日最新資訊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<>();    }    // 添加子節(jié)點    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();    }}

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

結構

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

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

模式應用后案例

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

public interface IComponent {//接口    void display();}public class File implements IComponent{//文件實現(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{//目錄實現(xiàn)    private String name;    private final List<IComponent> children;    // 初始化方法    public Directory(String name){        this.name = this.name;        this.children = new ArrayList<>();    }    // 添加子節(jié)點    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();    }}

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

適用場景

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

模式可能存在的困惑

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

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

本質

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

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

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

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

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

標簽:
  • 熱門焦點
Top