- 組合模式
- 透明模式與安全模式
- 對組合的篩選遍歷
無論是在生活中還是專案中,我們經常會遇到具有“部分-整體”概念的物件,比如員工與團隊的關系,這就類似樹形結構,可能具有很多的嵌套層次和分支,把這種復雜性直接暴露給呼叫端是不合適的,
組合模式
借助組合模式,可以將這類具有“部分-整體”的物件組合成樹形的層次結構,并使得用戶可以對單個物件和組合物件采用相同的使用方式,
GOF對組合模式的描述為:
Compose objects into tree structures to represent part-whole hierarchies.
Compositelets clients treat individual objects and compositions of objects uniformly.
— Design Patterns : Elements of Reusable Object-Oriented Software
UML類圖:

組合模式包含三個角色:
- Leaf:葉子節點,代表單個個體,它沒有子節點,
- Composite:組合節點,既可以包含葉子節點,也可以包含其他的組合節點,
- Component:抽象構件,定義Leaf和Composite共有的方法和屬性,可以定義一些默認的行為或屬性,
透明模式與安全模式
在使用組合模式時,根據抽象構件類的定義形式,可將組合模式分為透明模式和安全組合兩種形式,
透明模式
透明模式中,抽象構件Component中宣告了所有用于管理成員物件的方法,包括add()、remove()以及getChildren()等方法,這樣做的好處是確保所有的構件類都有相同的介面,在客戶端看來,葉子物件與容器物件所提供的方法是一致的,客戶端可以相同地對待所有的物件,透明組合模式也是組合模式的標準形式,前面的類圖表示的就是透明模式,
透明模式的缺點是不夠安全,因為葉子物件和容器物件在本質上是有區別的,葉子物件不可能有下一個層次的物件,即不可能包含成員物件,因此為其提供add()、remove()以及getChildren()等方法是沒有意義的,這在編譯階段不會出錯,但在運行階段如果呼叫這些方法就會導致例外,
透明模式的實作代碼如下:
public abstract class Component
{
protected IList<Component> children;
public virtual string Name { get; set; }
public virtual void Add(Component child)
{
children.Add(child);
}
public virtual void Remove(Component child)
{
children.Remove(child);
}
public virtual Component this[int index]
{
get { return children[index]; }
}
}
public class Leaf : Component
{
public override void Add(Component child)
{
throw new NotSupportedException();
}
public override void Remove(Component child)
{
throw new NotSupportedException();
}
public override Component this[int index] => throw new NotSupportedException();
}
public class Composite : Component
{
public Composite()
{
base.children = new List<Component>();
}
}
安全模式
安全模式則是將管理成員物件的方法從抽象構件Component轉移到了Composite,在抽象構件Component中沒有宣告任何用于管理成員物件的方法,這樣可以保證安全,葉子物件中無法呼叫到那些管理成員物件的方法,
安全模式的缺點是不夠透明,因為葉子構件和容器構件具有不同的方法,且容器構件中那些用于管理成員物件的方法沒有在抽象構件類中定義,因此客戶端不能完全針對抽象編程,必須有區別地對待葉子構件和容器構件,
對組合的篩選遍歷
將物件組合成樹形結構后,要使用這些物件,就需要用遍歷樹形結構的方式來獲取這些物件,
比如對于上面代碼中的Component,如果需要獲取全部結點的Names屬性
實作代碼可以為:
public List<string> names = new List<string>();
public virtual IEnumerable<string> GetNameList()
{
GetNameList(names);
return names;
}
private virtual void GetNameList(List<string> names)
{
names.Add(this.Name);
if (children != null && children.Count > 0)
{
foreach (Component child in children)
{
child.GetNameList(names);
}
}
}
但有的時候往往會遇到一些定制化的遍歷需求,比如只獲取Leaf結點(僅列出一個所有員工的名單),只獲取Composite結點(僅列出所有部門領導的資訊)等等,對于這些需求如果一一實作比較麻煩,且需要頻繁變化,可以采用一種更通用的方式,類似Linq中Where篩選那樣,呼叫的同時把篩選條件也傳入,
對GetNameList方法的擴展:
public virtual IEnumerable<string> GetNameList(Func<Component, bool> isMatchFunc)
{
GetNameList(names, isMatchFunc);
return names;
}
public virtual void GetNameList(List<string> names, Func<Component, bool> isMatchFunc)
{
if (isMatchFunc == null || isMatchFunc(this))
{
names.Add(this.Name);
}
if (children != null && children.Count > 0)
{
foreach (Component child in children)
{
child.GetNameList(names, isMatchFunc);
}
}
}
參考書籍:
王翔著 《設計模式——基于C#的工程化實作及擴展》
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/4554.html
標籤:設計模式
上一篇:二十三種設計模式修煉手冊
