組合模式,将對象組合成樹形結構以表示“部分-整體”的層次結構,組合模式使得使用者對單個對象群組合對象的使用具有一緻性。
解決整合與部分可以被一緻對待問題。

Component類:組合中的對象聲明接口,在适當情況下,實作所有類共有接口的行為。聲明一個接口用于通路和管理Component的子部件
Leaf類:葉節點對象,葉節點沒有子節點。由于葉節點不能增加分支和樹葉,是以葉節點的Add和Remove沒有實際意義。
有葉節點行為,用來存儲葉節點集合
Composite類:實作Componet的相關操作,比如Add和Remove操作。
children:用來存儲葉節點集合
1、抽象類Component
public 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 Diaplay(int depth);
}
2、葉子節點Leaf 繼承于Component
public class Leaf:Component
public Leaf(string name)
:base(name)
public override void Add(Component c)
Console.WriteLine("不能向葉子節點添加子節點");
public override void Remove(Component c)
Console.WriteLine("葉子節點沒有子節點");
public override void Diaplay(int depth)
Console.WriteLine(new string('-',depth)+name);
3、組合類Composite繼承于Component,擁有枝節點行為
public class Composite : Component
List<Component> children;
public Composite(string name)
if (children == null)
{
children = new List<Component>();
}
this.children.Add(c);
this.children.Remove(c);
Console.WriteLine(new String('-',depth)+name);
foreach (Component component in children)
component.Diaplay(depth + 2);
4、用戶端代碼
static void Main(string[] args)
Composite root = new Composite("根節點root");
root.Add(new Leaf("根上生出的葉子A"));
root.Add(new Leaf("根上生出的葉子B"));
Composite comp = new Composite("根上生出的分支CompositeX");
comp.Add(new Leaf("分支CompositeX生出的葉子LeafXA"));
comp.Add(new Leaf("分支CompositeX生出的葉子LeafXB"));
root.Add(comp);
Composite comp2 = new Composite("分支CompositeX生出的分支CompositeXY");
comp2.Add(new Leaf("分支CompositeXY生出葉子LeafXYA"));
comp2.Add(new Leaf("分支CompositeXY生出葉子LeafXYB"));
comp.Add(comp2);
root.Add(new Leaf("根節點生成的葉子LeafC"));
Leaf leafD = new Leaf("leaf D");
root.Add(leafD);
root.Remove(leafD);
root.Diaplay(1);
Console.Read();
假設公司組織結構為:
--總結理
----技術部門經理
------開發人員A
------開發人員B
----銷售部門經理
總經理直接上司技術部經理和銷售部經理,技術部經理直接上司開發人員A和開發人員B。銷售部經理暫時沒有直接下屬員工,随着公司規模增大,銷售部門會新增銷售員工。計算組織結構的總工資狀況。
如下圖所示
IComponent接口:此接口包括了Component和Composite的所有屬性,公司每個角色都有職稱Title和工資待遇Salary,Add方法把員工加入到組織團隊中。
Component葉子節點:葉節點沒有子節點,Add方法實作沒有任何意義。
Composite組合類:此類有一個員工集合_listEmployees,Add方法向此集合中添加員工資訊。
GetCost方法獲得組織結構中的工資待遇總和
1、接口IComponent
public interface IComponent
{
string Title { get; set; }
decimal Salary { get; set; }
void Add(IComponent c);
void GetCost(ref decimal salary);
}
8.
2、葉節點Component
public class Component : IComponent
public string Title { get; set; }
public decimal Salary { get; set; }
public Component(string Title, decimal Salary)
{
this.Title = Title;
this.Salary = Salary;
}
public void Add(IComponent c)
Console.WriteLine("Cannot add to the leaf!");
public void GetCost(ref decimal salary)
salary += Salary;
22.
3、組合類Composite
1. public class Composite : IComponent
2. {
3. private List<IComponent> _listEmployees;
4.
5. public string Title { get; set; }
6. public decimal Salary { get; set; }
7.
8. public Composite(string Title, decimal Salary)
9. {
10. this.Title = Title;
11. this.Salary = Salary;
12. _listEmployees = new List<IComponent>();
13. }
14.
15. public void Add(IComponent comp)
16. {
17. _listEmployees.Add(comp);
18. }
19.
20. public void GetCost(ref decimal salary)
21. {
22. salary += this.Salary;
23.
24. foreach (IComponent component in this._listEmployees)
25. {
26. component.GetCost(ref salary);
27. }
28. }
29. }
static void Main(string[] args)
decimal costCEO = 0.0M;
decimal costVPD = 0.0M;
//Create CEO Node
IComponent compCEO = new Composite("CEO", 500000);
//Create VP-Development and Developer nodes
IComponent compVPDev = new Composite("VP-Development", 250000);
IComponent compDev1 = new Component("Developer1", 75000);
IComponent compDev2 = new Component("Developer2", 50000);
compVPDev.Add(compDev1);
compVPDev.Add(compDev2);
//Create VP-Sales node
IComponent compVPSales = new Component("VP-Sales", 300000);
compCEO.Add(compVPDev);
compCEO.Add(compVPSales);
//Get the cost incurred at the CEO level
compCEO.GetCost(ref costCEO);
Console.WriteLine(String.Format("The Cost incurred at the CEO level is {0:c} ", costCEO));
//Get the cost incurred at the VP-Development level
compVPDev.GetCost(ref costVPD);
Console.WriteLine(String.Format("The Cost incurred at the VP-Development level is {0:c} ", costVPD));
33.
組合模式,将對象組合成樹形結構以表示“部分-整體”的層次結構,組合模式使得使用者對單個對象群組合對象的使用具有一緻性。解決整合與部分可以被一緻對待問題。
版權
作者:靈動生活 郝憲玮
如果你認為此文章有用,請點選底端的【推薦】讓其他人也了解此文章,
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。