Java結(jié)構(gòu)型模式中的組合模式詳解
一.介紹
組合模式(Composite Pattern)屬于結(jié)構(gòu)型模式。組合模式又叫作部分整體模式,它是一種將對(duì)象組合成樹(shù)狀層次結(jié)構(gòu)的模式,用來(lái)表示整體-部分的關(guān)系,使用戶(hù)對(duì)單個(gè)對(duì)象和組合對(duì)象具有一致的訪(fǎng)問(wèn)性。組合模式有透明方式和安全方式兩種實(shí)現(xiàn)方式
二.UML類(lèi)圖
1.透明方式
- 抽象節(jié)點(diǎn)中定義了規(guī)范,客戶(hù)端無(wú)需區(qū)別葉子節(jié)點(diǎn)和樹(shù)枝節(jié)點(diǎn),使用方便
- 葉子節(jié)點(diǎn)本來(lái)無(wú)add、remove、getChild方法,但是因?yàn)槌橄笾卸x了規(guī)范,所以必須實(shí)現(xiàn)它們,會(huì)帶來(lái)一些安全性問(wèn)題
2.安全方式
- 客戶(hù)端需要區(qū)別葉子節(jié)點(diǎn)和樹(shù)枝節(jié)點(diǎn),使用不方便
- 葉子節(jié)點(diǎn)無(wú)add、remove、getChild方法,不存在安全性問(wèn)題
三.具體代碼
業(yè)務(wù)代碼
//抽象節(jié)點(diǎn) public abstract class Component { abstract void add(Component component); abstract void remove(Component component); abstract Component getChild(int i); abstract void operation(); } //葉子節(jié)點(diǎn) class Leaf extends Component{ private String name; public Leaf(String name) { this.name = name; } @Override void add(Component component) {} @Override void remove(Component component) {} @Override Component getChild(int i) { return null; } @Override void operation() { System.out.print(name); } } //樹(shù)枝節(jié)點(diǎn) class Composite extends Component{ private ArrayList<Component> children = new ArrayList<>(); @Override void add(Component component) { children.add(component); } @Override void remove(Component component) { children.remove(component); } @Override Component getChild(int i) { return children.get(i); } @Override void operation() { children.forEach(Component::operation); } }
客戶(hù)端
public class Client { public static void main(String[] args) { Component level1 = new Composite(); level1.add(new Leaf("1")); level1.add(new Leaf("2")); Component level2 = new Composite(); level2.add(new Leaf("2.1")); level1.add(level2); level1.operation(); } }
四.使用場(chǎng)景
- 需要表示一個(gè)對(duì)象整體與部分的層次結(jié)構(gòu)
- 要求對(duì)用戶(hù)隱藏組合對(duì)象與單個(gè)對(duì)象的不同,用戶(hù)可以使用統(tǒng)一的接口操作組合結(jié)構(gòu)中的所有對(duì)象
- 組織機(jī)構(gòu)樹(shù)
- 文件/文件夾
五.優(yōu)點(diǎn)
- 簡(jiǎn)化了客戶(hù)端代碼(客戶(hù)端一致地處理單個(gè)對(duì)象和組合對(duì)象)
- 符合開(kāi)閉原則(新增具體實(shí)現(xiàn)類(lèi)不需要更改源代碼)
到此這篇關(guān)于Java結(jié)構(gòu)型模式中的組合模式詳解的文章就介紹到這了,更多相關(guān)Java組合模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 多線(xiàn)程饑餓現(xiàn)象的問(wèn)題解決方法
這篇文章主要介紹了java 多線(xiàn)程饑餓現(xiàn)象的問(wèn)題解決方法的相關(guān)資料,需要的朋友可以參考下2017-06-06Java實(shí)現(xiàn)實(shí)時(shí)監(jiān)控目錄下文件變化的方法
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)實(shí)時(shí)監(jiān)控目錄下文件變化的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03如何使用Java模擬退火算法優(yōu)化Hash函數(shù)
為了解決局部最優(yōu)解問(wèn)題,1983年,Kirkpatrick等提出了模擬退火算法(SA)能有效的解決局部最優(yōu)解問(wèn)題。模擬退火算法包含兩個(gè)部分即Metropolis算法和退火過(guò)程。Metropolis算法就是如何在局部最優(yōu)解的情況下讓其跳出來(lái),是退火的基礎(chǔ)2021-06-06java設(shè)計(jì)模式之簡(jiǎn)單工廠(chǎng)模式
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之簡(jiǎn)單工廠(chǎng)模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12spring使用JavaConfig進(jìn)行配置的方法
這篇文章主要介紹了spring使用JavaConfig進(jìn)行配置的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的方法
這篇文章主要給大家介紹了關(guān)于mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09如何把第三方服務(wù)注冊(cè)到spring項(xiàng)目容器中
這篇文章主要為大家介紹了如何把第三方服務(wù)注冊(cè)到spring項(xiàng)目容器中,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07解決idea web工程修改js文件之后不變化的問(wèn)題
這篇文章主要介紹了解決idea web工程修改js文件之后不變化的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12