Java描述设计模式(10):组合模式
生活随笔
收集整理的這篇文章主要介紹了
Java描述设计模式(10):组合模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文源碼:GitHub·點這里 || GitEE·點這里
一、生活場景
1、文件系統
下圖是常見的計算機文件系統的一部分。
文件系統是一個樹結構,樹上長有節點。樹的節點有兩種:
- 樹枝節點
即文件夾,有內部樹結構,在圖中涂有顏色;
- 樹葉節點
另一種是文件,即樹葉節點,沒有內部樹結構。
2、打印文件樹結構
public class C01_InScene {public static void main(String[] args) {File file = new File("F:\\tree") ;fileTree(file, 0);}private static void fileTree(File file, int floor) {// 判斷是否存在if (file.exists()) {if (floor > 0) {// 循環打空格for (int i = 0; i < floor; i++) {System.out.print(" ");}}if (file.isDirectory()) {System.out.println("+" + file.getName());// 列出所有文件及文件夾File[] files = file.listFiles();if (null != files) {// 循環遞歸for (File dirFile : files) {fileTree(dirFile, floor + 1);}}} else {System.out.println("-" + file.getName());}}} }執行效果:+代表文件夾,-代表文件。
+tree+dir1+dir2-dir2Leaf.txt-leaf1.txt-leaf2.txt-OneLeaf.txt-TwoLeaf.txt3、組合模式描述
組合模式屬于對象的結構模式,有時又叫做“部分——整體”模式。組合模式將對象組織到樹結構中,可以用來描述整體與部分的關系。組合模式可以使客戶端將單純元素與復合元素同等看待。
二、組合模式-安全式
1、基礎概念
安全式的組合模式要求管理聚集的方法只出現在樹枝構件類中,而不出現在樹葉構件類中。涉及到三個角色:
- 抽象構件(Component)角色
它給組合的對象定義出公共的接口及其默認行為,可以用來管理所有的子對象。組合對象通常把它所包含的子對象當做類型為Component的對象。在安全式的組合模式里,構件角色并不定義出管理子對象的方法,這一定義由樹枝構件對象給出。
- 樹葉構件(Leaf)角色
樹葉對象是沒有下級子對象的對象,定義出參加組合的原始對象的行為。
- 樹枝構件(Composite)角色
代表參加組合的有下級子對象的對象。樹枝構件類給出所有的管理子對象的方法,如add()、remove()以及getChild()。
2、模式圖解
3、源代碼實現
public class C02_Security_Model {public static void main(String[] args) {Composite root = new Composite("服裝");Composite composite1 = new Composite("男裝");Leaf manCoat = new Leaf("上衣");Leaf manBottom = new Leaf("下衣");composite1.addChild(manCoat);composite1.addChild(manBottom);Composite composite2 = new Composite("女裝");Leaf leaf1 = new Leaf("鞋子");Leaf leaf2 = new Leaf("帽子");root.addChild(leaf1);root.addChild(leaf2);root.addChild(composite1);root.addChild(composite2);root.printStruct("");} } // 抽象構件角色類 interface Component {/** 輸出組件自身的名稱*/void printStruct(String preStr); } // 樹枝構件角色類 class Composite implements Component{// 用來存儲組合對象中包含的子組件對象private List<Component> childComponents = new ArrayList<Component>();// 輸出對象的名稱private String name;// 構造方法,傳入組合對象的名字public Composite (String name){this.name = name;}/*** 聚集管理方法,增加一個子構件對象* @param child 子構件對象*/public void addChild(Component child){childComponents.add(child);}/*** 聚集管理方法,刪除一個子構件對象* @param index 子構件對象的下標*/public void removeChild(int index){childComponents.remove(index);}/*** 聚集管理方法,返回所有子構件對象*/public List getChild(){return childComponents ;}/*** 輸出對象的自身結構* @param preStr 前綴,主要是按照層級拼接空格,實現向后縮進*/@Overridepublic void printStruct(String preStr) {//先輸出自己System.out.println(preStr+"+"+this.name);//如果還包含有子組件,那么就輸出這些子組件對象if (this.childComponents != null){//添加兩個空格,表示向后縮進兩個空格preStr = preStr+" ";//輸出當前的子對象:使用函數遞歸的原理for (Component c : childComponents) {c.printStruct(preStr);}}} } class Leaf implements Component{// 輸出葉子對象的名稱private String name;// 構造方法,傳入葉子對象的名稱public Leaf (String name){this.name = name ;}/*** 輸出葉子對象的結構,葉子對象沒有子對象,也就是輸出葉子對象的名字* @param preStr 前綴,主要是按照層級拼接的空格,實現向后縮進*/@Overridepublic void printStruct(String preStr) {System.out.println(preStr+"-"+name);} }- 輸出結果
三、組合模式-透明式
1、概念圖解
與安全式的組合模式不同的是,透明式的組合模式要求所有的具體構件類,不論樹枝構件還是樹葉構件,均符合一個固定接口。
2、源代碼實現
public class C03_Transparent_Model {public static void main(String[] args) {Component1 root = new Composite1("服裝");Component1 c1 = new Composite1("男裝");Component1 c2 = new Composite1("女裝");Component1 leaf1 = new Leaf1("襯衫");Component1 leaf2 = new Leaf1("夾克");Component1 leaf3 = new Leaf1("裙子");Component1 leaf4 = new Leaf1("套裝");root.addChild(c1);root.addChild(c2);c1.addChild(leaf1);c1.addChild(leaf2);c2.addChild(leaf3);c2.addChild(leaf4);root.printStruct("");} } abstract class Component1 {/*** 輸出組件自身的名稱*/public abstract void printStruct(String preStr);// 聚集管理方法,增加一個子構件對象public void addChild(Component1 child){/*** 缺省實現,拋出異常,因為葉子對象沒有此功能* 或者子組件沒有實現這個功能*/throw new UnsupportedOperationException("對象不支持此功能");}// 聚集管理方法,刪除一個子構件對象public void removeChild(int index){/*** 缺省實現,拋出異常,因為葉子對象沒有此功能* 或者子組件沒有實現這個功能*/throw new UnsupportedOperationException("對象不支持此功能");}// 聚集管理方法,返回所有子構件對象public List<Component1> getChild(){/*** 缺省實現,拋出異常,因為葉子對象沒有此功能* 或者子組件沒有實現這個功能*/throw new UnsupportedOperationException("對象不支持此功能");} } class Composite1 extends Component1 {// 用來存儲組合對象中包含的子組件對象private List<Component1> childComponents = new ArrayList<Component1>();// 輸出對象名稱private String name ;public Composite1 (String name){this.name = name;}/*** 聚集管理方法,增加一個子構件對象* @param child 子構件對象*/public void addChild(Component1 child){childComponents.add(child);}/*** 聚集管理方法,刪除一個子構件對象* @param index 子構件對象的下標*/public void removeChild(int index){childComponents.remove(index);}// 聚集管理方法,返回所有子構件對象public List<Component1> getChild(){return childComponents ;}/*** 輸出對象的自身結構* @param preStr 前綴,主要是按照層級拼接空格,實現向后縮進*/@Overridepublic void printStruct(String preStr) {// 首先輸出自己名稱System.out.println(preStr+"+"+this.name);// 如果還包含有子組件,那么就輸出這些子組件對象preStr = preStr + " ";if (this.childComponents != null) {// 添加兩個空格,表示向后縮進for (Component1 c : childComponents) {遞歸輸出每個子對象c.printStruct(preStr);}}} } class Leaf1 extends Component1 {private String name;public Leaf1 (String name){this.name = name;}/*** 輸出葉子對象的結構,葉子對象沒有子對象,也就是輸出葉子對象的名字* @param preStr 前綴,主要是按照層級拼接的空格,實現向后縮進*/@Overridepublic void printStruct(String preStr) {System.out.println(preStr+"-"+name);} }四、JDK中應用
1、HashMap結構圖
2、分層結構
- interface Map
- class AbstractMap implements Map
- HashMap extends AbstractMap implements Map
- interface Map.Entry
- Node implements Map.Entry
3、源代碼
- 存儲葉子節點
- 存儲樹枝節點
五、源代碼地址
GitHub·地址 https://github.com/cicadasmile/model-arithmetic-parent GitEE·地址 https://gitee.com/cicadasmile/model-arithmetic-parent總結
以上是生活随笔為你收集整理的Java描述设计模式(10):组合模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TACACS +和RADIUS比较
- 下一篇: Java并发编程(06):Lock机制下