java設(shè)計模式之組合模式(Composite)
概述
是一種結(jié)構(gòu)型模式,將對象以樹形結(jié)構(gòu)組織起來,以表示“部分 - 整體”的層次結(jié)構(gòu),使得客戶端對單個對象和組合對象的使用具有唯一性。
UML類圖
上面的類圖包含的角色:
Component:為參加組合的對象聲明一個公共的接口,不管是組合還是葉節(jié)點(diǎn)。
Leaf:在組合中表示葉子結(jié)點(diǎn)對象,葉子結(jié)點(diǎn)沒有子結(jié)點(diǎn)。
Composite:表示參加組合的有子對象的對象,并給出樹枝構(gòu)建的行為;
代碼示例
import java.util.ArrayList; import java.util.List; abstract class Component { protected String name; public Component(String name) { this.name = name; } public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void GetChild(int depth); } class Leaf extends Component { public Leaf(String name) { super(name); } @Override public void Add(Component c) { System.out.println("Can not add to a leaf"); } @Override public void Remove(Component c) { System.out.println("Can not remove from a leaf"); } @Override public void GetChild(int depth) { String temp = " "; for (int i = 0; i < depth; i++) { temp += "-"; System.out.println(temp + name); } } } class Composite extends Component { private List<Component> children = new ArrayList<>(); public Composite(String name) { super(name); } @Override public void Add(Component c) { children.add(c); } @Override public void Remove(Component c) { children.remove(c); } @Override public void GetChild(int depth) { for (Component c : children) { c.GetChild(depth); } } } public class Main { public static void main(String args[]) { Composite root = new Composite("root"); root.Add(new Leaf("Leaf A")); root.Add(new Leaf("Leaf B")); Composite compX = new Composite("Composite X"); compX.Add(new Leaf("Leaf XA")); compX.Add(new Leaf("Leaf XB")); root.Add(compX); Composite compXY = new Composite("Composite XY"); compXY.Add(new Leaf("Leaf XYA")); compXY.Add(new Leaf("Leaf XYB")); compX.Add(compXY); root.GetChild(3); } }
應(yīng)用場景
1.要求表示對象的部分-整體層次結(jié)構(gòu)。
2.想要客戶端忽略組合對象與單個對象的差異,客戶端將統(tǒng)一地使用組合結(jié)構(gòu)中的所有對象。
組合模式定義由Leaf對象和Composite對象組成的類結(jié)構(gòu);
使得客戶端變得簡單;
它使得添加或刪除子部件變得很容易。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解
這篇文章主要介紹了Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解,interrupt用于中斷線程,調(diào)用該方法的線程的狀態(tài)將會被設(shè)置為中斷狀態(tài),線程中斷僅僅是設(shè)置線程的中斷狀態(tài)位,并不會停止線程,需要用戶自己去監(jiān)視線程的狀態(tài)并作出處理,需要的朋友可以參考下2023-12-12Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理
在本篇文章里小編給各位整理了關(guān)于Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題相關(guān)內(nèi)容,有興趣的朋友們跟著參考學(xué)習(xí)下。2019-07-07Idea中Java項(xiàng)目如何修改項(xiàng)目名
這篇文章主要介紹了Idea中Java項(xiàng)目如何修改項(xiàng)目名問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Java8利用Stream實(shí)現(xiàn)列表去重的方法詳解
這篇文章主要為大家介紹了Java利用Stream實(shí)現(xiàn)列表去重的幾種方法詳解,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2022-04-04spring 自動注入AutowiredAnnotationBeanPostProcessor源碼解析
這篇文章主要介紹了spring自動注入AutowiredAnnotationBeanPostProcessor源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03一文學(xué)會使用sa-token解決網(wǎng)站權(quán)限驗(yàn)證
這篇文章主要為大家介紹了使用sa-token解決網(wǎng)站權(quán)限驗(yàn)證方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07