java組合模式的結(jié)果和適用的場(chǎng)景

本篇內(nèi)容主要講解“java組合模式的結(jié)果和適用的場(chǎng)景”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java組合模式的結(jié)果和適用的場(chǎng)景”吧!

創(chuàng)新互聯(lián)是一家專業(yè)提供塔河企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、網(wǎng)站制作、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為塔河眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。

組合(Composite)模式的定義:有時(shí)又叫作部分-整體模式,它是一種將對(duì)象組合成樹狀的層次結(jié)構(gòu)的模式,用來表示“部分-整體”的關(guān)系。組合模式使得客戶端代碼可以一致地處理單個(gè)對(duì)象和組合對(duì)象,無須關(guān)心自己處理的是單個(gè)對(duì)象,還是組合對(duì)象,這簡(jiǎn)化了客戶端代碼;

模式結(jié)構(gòu)

  • 頂層抽象:樹枝或者樹葉的抽象接口

  • 樹枝:是組合中的葉節(jié)點(diǎn)對(duì)象,它沒有子節(jié)點(diǎn),用于實(shí)現(xiàn)抽象構(gòu)件角色中 聲明的公共接口。

  • 樹葉:是組合中的分支節(jié)點(diǎn)對(duì)象,它有子節(jié)點(diǎn)。它實(shí)現(xiàn)了抽象構(gòu)件角色中聲明的接口,它的主要作用是存儲(chǔ)和管理子部件

源碼導(dǎo)讀

組合模式分為透明模式和安全模式;透明模式是在頂層抽象中聲明了所有管理子對(duì)象的方法,樹葉節(jié)點(diǎn)點(diǎn)和樹枝節(jié)點(diǎn)對(duì)于客戶端來說沒有區(qū)別。安全模式是在頂層抽象中只聲明葉子和樹枝公有的抽象方法,而將對(duì)葉子和樹枝的管理方法實(shí)現(xiàn)到對(duì)應(yīng)的類中,因此客戶端就需要區(qū)分該節(jié)點(diǎn)是樹枝還是葉子從而調(diào)用對(duì)應(yīng)的方法。

對(duì)組合模式來說,List Set等這些集合類屬于不那么嚴(yán)格的組合模式。由于沒有找到太好的源碼,因此我在這里分別對(duì)透明模式和安全模式組合說明

透明模式:

public abstract class Component{
    private String name;
    public Component(string name)
    {
        this.name = name;
    }
 
    public abstract boolean Add(Component component);
 
    public abstract boolean Remove(Component component);
 
    public String getName(){
        return name;
    }
}

public class Branch extend Component{
    private List<Component> tree=new ArrayList<>();
    
    public Branch(String name){
        super(name);
    }
    
    public boolean add(Componet component){
        tree.add(component);
        return true;
    }
    
    public boolean Remove(Component component){
        tree.remove(component);
        return true;
    }
}

public class Leaf extend Component{
    
     public Leaf(String name){
        super(name);
    }
     
     public boolean add(Componet component){
        return false;
    }
    
    public boolean Remove(Component component){
        return false;
    }
    
}

安全模式:

public abstract class Component{
    private String name;
    public Component(string name)
    {
        this.name = name;
    }
 
    public String getName(){
        return name;
    }
    
    
}

public class Branch extend Component{
    private List<Component> tree=new ArrayList<>();
    
    public Branch(String name){
        super(name);
    }
    
    public boolean add(Componet component){
        tree.add(component);
        return true;
    }
    
    public boolean Remove(Component component){
        tree.remove(component);
        return true;
    }
    
    public List<Component> getTree(){
        return tree;
    }
}

public class Leaf extend Component{
    
     public Leaf(String name){
        super(name);
    }
    
    
}

組合模式適用的場(chǎng)景為需要表述一個(gè)系統(tǒng)(組件)的整體與部分的結(jié)構(gòu)層次的場(chǎng)合;組合模式可對(duì)客戶端隱藏組合對(duì)象和單個(gè)對(duì)象的不同,以便客戶端可以使用用統(tǒng)一的接口使用組合結(jié)構(gòu)中的所有對(duì)象,對(duì)于該類場(chǎng)合也適用于組合模式

到此,相信大家對(duì)“java組合模式的結(jié)果和適用的場(chǎng)景”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

當(dāng)前名稱:java組合模式的結(jié)果和適用的場(chǎng)景
網(wǎng)頁網(wǎng)址:http://muchs.cn/article32/johepc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)公司、外貿(mào)建站、營(yíng)銷型網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計(jì)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化