日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

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

一文徹底搞明白組合模式

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

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

定義

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

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

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

模式應用前案例

下面以一個典型的文件和目錄為例來進行說明,先來看一下未應用組合模式之前的代碼實現。ZNg28資訊網——每日最新資訊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();    }}

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

結構

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

組合模式的示例代碼如下。ZNg28資訊網——每日最新資訊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();    }}

模式應用后案例

上面文件與目錄的案例,使用組合模式之后的代碼實現如下。ZNg28資訊網——每日最新資訊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();    }}

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

適用場景

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

模式可能存在的困惑

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

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

本質

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

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

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

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

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

標簽:
  • 熱門焦點
  • K60至尊版狂暴引擎2.0加持:超177萬跑分斬獲性能第一

    Redmi的后性能時代戰略發布會今天下午如期舉辦,在本次發布會上,Redmi公布了多項關于和聯發科的深度合作,以及新機K60 Ultra在軟件和硬件方面的特性,例如:“K60 至尊版,雙芯旗艦
  • 0糖0卡0脂 旭日森林仙草烏龍茶優惠:15瓶到手29元

    旭日森林無糖仙草烏龍茶510ml*15瓶平時要賣為79.9元,今日下單領取50元優惠券,到手價為29.9元。產品規格:0糖0卡0脂,添加草本仙草汁,清涼爽口,富含茶多酚,保留
  • 如何正確使用:Has和:Nth-Last-Child

    我們可以用CSS檢查,以了解一組元素的數量是否小于或等于一個數字。例如,一個擁有三個或更多子項的grid。你可能會想,為什么需要這樣做呢?在某些情況下,一個組件或一個布局可能會
  • 三言兩語說透柯里化和反柯里化

    JavaScript中的柯里化(Currying)和反柯里化(Uncurrying)是兩種很有用的技術,可以幫助我們寫出更加優雅、泛用的函數。本文將首先介紹柯里化和反柯里化的概念、實現原理和應用
  • JavaScript學習 -AES加密算法

    引言在當今數字化時代,前端應用程序扮演著重要角色,用戶的敏感數據經常在前端進行加密和解密操作。然而,這樣的操作在網絡傳輸和存儲中可能會受到惡意攻擊的威脅。為了確保數據
  • 三萬字盤點 Spring 九大核心基礎功能

    大家好,我是三友~~今天來跟大家聊一聊Spring的9大核心基礎功能。話不多說,先上目錄:圖片友情提示,本文過長,建議收藏,嘿嘿嘿!一、資源管理資源管理是Spring的一個核心的基礎功能,不
  • OPPO K11評測:旗艦級IMX890加持 2000元檔最強影像手機

    【Techweb評測】中端機型用戶群體巨大,占了中國目前手機市場的大頭,一直以來都是各手機品牌的“必爭之地”,其中OPPO K系列機型一直以來都以高品質、
  • 質感不錯!OPPO K11渲染圖曝光:旗艦IMX890傳感器首次下放

    一直以來,OPPO K系列機型都保持著較為均衡的產品體驗,歷來都是2K價位的明星機型,去年推出的OPPO K10和OPPO K10 Pro兩款機型憑借各自的出色配置,堪稱有
  • DRAM存儲器10月價格下跌,NAND閃存本月價格與上月持平

    10月30日,據韓國媒體消息,自今年年初以來一直在上漲的 DRAM 存儲器的交易價格僅在本月就下跌了近 10%,此次是全年首次降價,而NAND 閃存本月價格與上月持平。市
Top 主站蜘蛛池模板: 临高县| 林芝县| 五峰| 遵义县| 顺义区| 正宁县| 乐清市| 无棣县| 怀柔区| 隆德县| 凤山市| 澄迈县| 和林格尔县| 云梦县| 同德县| 虞城县| 大荔县| 穆棱市| 广南县| 德惠市| 资中县| 青海省| 东兰县| 墨脱县| 宜都市| 灵山县| 获嘉县| 淅川县| 威宁| 游戏| 合水县| 清镇市| 宁远县| 东乌| 宣恩县| 阿荣旗| 昭苏县| 招远市| 海宁市| 枣阳市| 高州市|