第0章:簡介
組合模式的定義:通過把葉子對象當成特殊的組合對象看待,從而對葉子對象和組合對象一視同仁,統統當成了Component對象,有機的統一了葉子對象和組合對象。
組合模式的本質:統一葉子對象和組合對象
參考:研磨設計模式(書籍),大話設計模式(書籍),圖解設計模式(書籍)
模式圖:
待補充
第1章:實踐
第0節:透明性的實現
(1)組件對象(Component.java)
package com.mcc.core.designPattern.structure.composite.transparentComponent; /** * 組件對象 * 葉子對象和組合對象的父類,使葉子對象和組合對象具有一致性 * 透明性的實現(把管理子組件的操作定義在Component中,客戶端只需要面對Component,而無需關心具體的組件類型,這種實現方式就是透明性的實現。 * 但是透明性的實現是以安全性爲代價的,因爲在Component中定義的一些方法,對於葉子對象來說是沒有意義的,比如:增加、移除子組件對象。) * * * 組合模式的定義:通過把葉子對象當成特殊的組合對象看待,從而對葉子對象和組合對象一視同仁,統統當成了Component對象,有機的統一了葉子對象和組合對象。 * 組合模式的本質:統一葉子對象和組合對象 * * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public abstract class Component { //葉子對象和組合對象都有的方法,只是具體實現不同 public abstract void method1(); //葉子對象和組合對象都有的方法,只是具體實現不同 //本例做輸出自己及其子對象 public abstract void method2(); /** * 增加子對象 * @param component */ public void addChild(Component component){ //缺省的實現,拋出例外,因爲葉子對象沒有這個功能,或者子組件沒有實現這個功能 throw new UnsupportedOperationException("對象不支持這個功能"); } /** * 移除子對象 * @param component */ public void removeChild(Component component){ //缺省的實現,拋出例外,因爲葉子對象沒有這個功能,或者子組件沒有實現這個功能 throw new UnsupportedOperationException("對象不支持這個功能"); } /** * 獲取子對象 * @param index * @return */ public Component getChildren(int index){ //缺省的實現,拋出例外,因爲葉子對象沒有這個功能,或者子組件沒有實現這個功能 throw new UnsupportedOperationException("對象不支持這個功能"); } } |
package com.mcc.core.designPattern.structure.composite.transparentComponent; import java.util.ArrayList; import java.util.List; /** * 組合對象 * 表示“容器”的對象 * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Composite extends Component { //用來存儲組合對象中包含的子組件對象 private List<Component> childComponents = null; @Override public void method1() { System.out.println("組合對象method方法"); } @Override public void method2() { System.out.println("我是組合對象:" + this + ",下面是我的子樹對象:"); if(childComponents != null && childComponents.size() > 0){ for(Component c : childComponents) { c.method2(); } System.out.println("------------------------------------------"); } } /** * 增加子對象 * 複寫父類方法 * @param component */ public void addChild(Component component){ //延遲初始化 if (childComponents == null) { childComponents = new ArrayList<Component>(); } childComponents.add(component); } /** * 移除子對象 * 複寫父類方法 * @param component */ public void removeChild(Component component){ if(childComponents != null){ childComponents.remove(component); } } /** * 獲取子對象 * 複寫父類方法 * @param index * @return */ public Component getChildren(int index){ if (childComponents != null){ if(index>=0 && index<childComponents.size()){ return childComponents.get(index); } } return null; } } |
(3)葉子對象(Leaf.java)
package com.mcc.core.designPattern.structure.composite.transparentComponent; /** * 葉子對象 * 表示“內容”的對象 * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Leaf extends Component { @Override public void method1() { System.out.println("葉子對象method1方法"); } @Override public void method2() { System.out.println("我是葉子對象:" + this); } } |
package com.mcc.core.designPattern.structure.composite.transparentComponent; /** * 客戶端測試 * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Client { public static void main(String args[]){ //定義多個組合對象 Component root = new Composite(); Component c1 = new Composite(); Component c2 = new Composite(); //定義多個葉子對象 Component leaf1 = new Leaf(); Component leaf2 = new Leaf(); Component leaf3 = new Leaf(); Component leaf4 = new Leaf(); //組成樹結構 root.addChild(c1); root.addChild(c2); root.addChild(leaf1); c1.addChild(leaf2); c2.addChild(leaf3); c2.addChild(leaf4); //輸出樹結構,從左到右遍歷樹 root.method2(); //不安全性展示 leaf1.addChild(leaf2); } } |
第1節:安全性的實現
(1)組件對象(Component.java)
package com.mcc.core.designPattern.structure.composite.securityComponent; /** * 組件對象 * 葉子對象和組合對象的父類,使葉子對象和組合對象具有一致性 * 安全性的實現(把管理子組件的操作定義在Composite中,那麼客戶在使用葉子對象的時候,就不會發生使用添加子組件或是刪除子組件的操作了,因爲壓根就沒有這樣的功能,這種實現方式是安全的。 * 但是這樣一來,客戶端在使用的時候,就必須區分到底使用的是Composite對象,還是葉子對象,不同對象的功能是不一樣的。也就是說,這種實現方式,對客戶而言就不是透明的了。) * * * 組合模式的定義:通過把葉子對象當成特殊的組合對象看待,從而對葉子對象和組合對象一視同仁,統統當成了Component對象,有機的統一了葉子對象和組合對象。 * 組合模式的本質:統一葉子對象和組合對象 * * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public abstract class Component { //葉子對象和組合對象都有的方法,只是具體實現不同 public abstract void method1(); //葉子對象和組合對象都有的方法,只是具體實現不同 //本例做輸出自己及其子對象 public abstract void method2(); } |
(2)組合對象(Composite.java)
package com.mcc.core.designPattern.structure.composite.securityComponent; import java.util.ArrayList; import java.util.List; /** * 組合對象 * 表示“容器”的對象 * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Composite extends Component { //用來存儲組合對象中包含的子組件對象 private List<Component> childComponents = null; @Override public void method1() { System.out.println("組合對象method方法"); } @Override public void method2() { System.out.println("我是組合對象:" + this + ",下面是我的子樹對象:"); if(childComponents != null && childComponents.size() > 0){ for(Component c : childComponents) { c.method2(); } System.out.println("------------------------------------------"); } } /** * 增加子對象 * @param component */ public void addChild(Component component){ //延遲初始化 if (childComponents == null) { childComponents = new ArrayList<Component>(); } childComponents.add(component); } /** * 移除子對象 * @param component */ public void removeChild(Component component){ if(childComponents != null){ childComponents.remove(component); } } /** * 獲取子對象 * @param index * @return */ public Component getChildren(int index){ if (childComponents != null){ if(index>=0 && index<childComponents.size()){ return childComponents.get(index); } } return null; } } |
(3)葉子對象(Leaf.java)
package com.mcc.core.designPattern.structure.composite.securityComponent; /** * 葉子對象 * 表示“內容”的對象 * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Leaf extends Component { @Override public void method1() { System.out.println("葉子對象method1方法"); } @Override public void method2() { System.out.println("我是葉子對象:" + this); } } |
(4)客戶端測試(Client.java)
package com.mcc.core.designPattern.structure.composite.securityComponent; /** * 客戶端測試 * * @author <a href="mailto:[email protected]">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Client { public static void main(String args[]){ //定義多個組合對象 Composite root = new Composite(); Composite c1 = new Composite(); Composite c2 = new Composite(); //定義多個葉子對象 Leaf leaf1 = new Leaf(); Leaf leaf2 = new Leaf(); Leaf leaf3 = new Leaf(); Leaf leaf4 = new Leaf(); //組成樹結構 root.addChild(c1); root.addChild(c2); root.addChild(leaf1); c1.addChild(leaf2); c2.addChild(leaf3); c2.addChild(leaf4); //輸出樹結構,從左到右遍歷樹 root.method2(); } } |