一、組合模式定義
將對象組合成樹形結構以表示“部分-整體”的層次結構,使得用戶對單個對象和組合對象的使用具有一致性。Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
?????
?
??? 如上圖所示(截取自《Head First Design Patterns》一書),主要包括三個部分:?
??? 1. Component抽象組件。定義參加組合對象的共有方法和屬性,可以定義一些默認的函數或屬性。?
????2. Leaf葉子節點。構成組合樹的最小構建單元。?
??? 3. Composite樹枝節點組件。它的作用是組合樹枝節點和葉子節點形成一個樹形結構。?
Component : 組合中的對象聲明接口,在適當的情況下,實現所有類共有接口的默認行為。聲明一個接口用于訪問和管理 Component 的子部件。
1 abstract class Component { 2 protected String name; 3 4 public Component(String name) { 5 this.name = name; 6 } 7 8 public abstract void Add(Component c); 9 public abstract void Remove(Component c); 10 public abstract void Display(int depth); 11 }
Leaf : 表示葉節點對象。葉子節點沒有子節點。
1 class Leaf extends Component { 2 3 public Leaf(String name) { 4 super(name); 5 } 6 7 @Override 8 public void Add(Component c) { 9 System.out.println("Can not add to a leaf"); 10 } 11 12 @Override 13 public void Remove(Component c) { 14 System.out.println("Can not remove from a leaf"); 15 } 16 17 @Override 18 public void Display(int depth) { 19 String temp = ""; 20 for (int i = 0; i < depth; i++) 21 temp += '-'; 22 System.out.println(temp + name); 23 } 24 25 }
?
Composite : 定義枝節點行為,用來存儲子部件,在 Component 接口中實現與子部件相關的操作。例如 Add 和 Remove。
1 class Composite extends Component { 2 3 private List<Component> children = new ArrayList<Component>(); 4 5 public Composite(String name) { 6 super(name); 7 } 8 9 @Override 10 public void Add(Component c) { 11 children.add(c); 12 } 13 14 @Override 15 public void Remove(Component c) { 16 children.remove(c); 17 } 18 19 @Override 20 public void Display(int depth) { 21 String temp = ""; 22 for (int i = 0; i < depth; i++) 23 temp += '-'; 24 System.out.println(temp + name); 25 26 for (Component c : children) { 27 c.Display(depth + 2); 28 } 29 } 30 31 }
Client : 通過 Component 接口操作結構中的對象。
1 public class CompositePattern { 2 3 public static void main(String[] args) { 4 Composite root = new Composite("root"); 5 root.Add(new Leaf("Leaf A")); 6 root.Add(new Leaf("Leaf B")); 7 8 Composite compX = new Composite("Composite X"); 9 compX.Add(new Leaf("Leaf XA")); 10 compX.Add(new Leaf("Leaf XB")); 11 root.Add(compX); 12 13 Composite compXY = new Composite("Composite XY"); 14 compXY.Add(new Leaf("Leaf XYA")); 15 compXY.Add(new Leaf("Leaf XYB")); 16 compX.Add(compXY); 17 18 root.Display(1); 19 } 20 21 }
二、組合模式優勢?
節點自由擴展增加。使用組合模式,如果想增加一個樹枝節點或者葉子節點都是很簡單的,只要找到它的父節點就可以了,非常容易擴展,符合“開閉原則”。應用最廣的模式之一。應用在維護和展示部分-整體關系的場景,如樹形菜單、文件夾管理等等。一棵樹形結構的所有節點都是Component,局部和整體對調用者來說都是一樣的,沒有區別,所以高層模塊不比關心自己處理的是單個對象還是整個組合結構,簡化了高層模塊的代碼。
? 1、可以清楚地定義分層次的復雜對象,表示對象的全部或部分層次,使得增加新構件也更容易。
????? 2、客戶端調用簡單,客戶端可以一致的使用組合結構或其中單個對象。
????? 3、定義了包含葉子對象和容器對象的類層次結構,葉子對象可以被組合成更復雜的容器對象,而這個容器對象又可以被組合,這樣不斷遞歸下去,可以形成復雜的樹形結構。
????? 4、更容易在組合體內加入對象構件,客戶端不必因為加入了新的對象構件而更改原有代碼。
組合模式的缺點: 使設計變得更加抽象,對象的業務規則如果很復雜,則實現組合模式具有很大挑戰性,而且不是所有的方法都與葉子對象子類都有關聯。
使用場景:
? ?1、需要表示一個對象整體或部分層次,在具有整體和部分的層次結構中,希望通過一種方式忽略整體與部分的差異,可以一致地對待它們。
?
????? 2、讓客戶能夠忽略不同對象層次的變化,客戶端可以針對抽象構件編程,無須關心對象層次結構的細節。
?
?三、組合模式在Android源碼中的應用?
??? 在Android源碼中,都能找到使用組合模式的例子,其中在《Android源碼學習之觀察者模式應用》介紹到的ViewGroup和View的結構就是一個組合模式,結構圖如下所示:?
?? ? 現在來看看它們是如何利用組合模式組織在一起的,首先在View類定義了有關具體操作,然后在ViewGroup類中繼承View類,并添加相關的增加、刪除和查找孩子View節點,代碼如下:?
/*
* @attr ref android.R.styleable#ViewGroup_clipChildren * @attr ref android.R.styleable#ViewGroup_clipToPadding * @attr ref android.R.styleable#ViewGroup_layoutAnimation * @attr ref android.R.styleable#ViewGroup_animationCache * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren * @attr ref android.R.styleable#ViewGroup_descendantFocusability * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges */ public abstract class ViewGroup extends View implements ViewParent, ViewManager {
?接著看增加孩子節點函數:?
/*** Adds a child view. If no layout parameters are already set on the child, the* default parameters for this ViewGroup are set on the child.** @param child the child view to add** @see #generateDefaultLayoutParams()*/public void addView(View child) { addView(child, -1); } /** * Adds a child view. If no layout parameters are already set on the child, the * default parameters for this ViewGroup are set on the child. * * @param child the child view to add * @param index the position at which to add the child * * @see #generateDefaultLayoutParams() */ public void addView(View child, int index) { LayoutParams params = child.getLayoutParams(); if (params == null) { params = generateDefaultLayoutParams(); if (params == null) { throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); } } addView(child, index, params); } /** * Adds a child view with this ViewGroup's default layout parameters and the * specified width and height. * * @param child the child view to add */ public void addView(View child, int width, int height) { final LayoutParams params = generateDefaultLayoutParams(); params.width = width; params.height = height; addView(child, -1, params); } /** * Adds a child view with the specified layout parameters. * * @param child the child view to add * @param params the layout parameters to set on the child */ public void addView(View child, LayoutParams params) { addView(child, -1, params); } /** * Adds a child view with the specified layout parameters. * * @param child the child view to add * @param index the position at which to add the child * @param params the layout parameters to set on the child */ public void addView(View child, int index, LayoutParams params) { if (DBG) { System.out.println(this