- 將物件組合樹形結構以表示「部份-整體」的層次結構。組合模式使得用戶對單個物件和組合物件的使用具有一致性。
- 當發現需求中是表現部份與整體層次的結構時,以及你希望用戶可以忽略組合物件與單個的不同,統一地組合結構中的所有物件時,就應該考慮用組合模式。

基本程式碼
using System;
using System.Collections.Generic;
namespace DPExample
{
// 組合中的物件宣告介面,實現所有類別共有介面的預定行為 //
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 Display (int depth);
}
// 葉節點
class Leaf : Component
{
public Leaf (string name) : base(name)
{}
public override void Add (Component c)
{
Console.WriteLine ("Cannot add to a leaf");
}
public override void Remove (Component c)
{
Console.WriteLine ("Cannot remove from a leaf");
}
public override void Display (int depth)
{
Console.WriteLine (new String('-', depth) + name);
}
}
// 枝節點
class Composite : Component
{
private List children = new List();
public Composite (String name) : base(name)
{}
public override void Add (Component c)
{
children.Add(c);
}
public override void Remove (Component c)
{
children.Remove (c);
}
public override void Display (int depth)
{
Console.WriteLine (new String('-', depth) + name);
foreach (Component component in children)
component.Display (depth + 2);
}
}
}
using System;
namespace DPExample
{
public class Program
{
static void Main ()
{
Composite root = new Composite("root");
root.Add (new Leaf("Leaf A"));
root.Add (new Leaf("Leaf B"));
Composite comp = new Composite("Composite X");
comp.Add (new Leaf("Leaf XA"));
comp.Add (new Leaf("Leaf XB"));
root.Add (comp);
Composite comp2 = new Composite("Composite XY");
comp2.Add (new Leaf("Leaf XYA"));
comp2.Add (new Leaf("Leaf XYB"));
comp.Add (comp2);
root.Add (new Leaf("Leaf C"));
Leaf leaf = new Leaf("Leaf D");
root.Add (leaf);
root.Remove (leaf);
root.Display(1);
}
}
}
實例程式碼
using System;
using System.Collections.Generic;
namespace DPExample
{
abstract class 公司
{
protected string name;
public 公司 (string name)
{
this.name = name;
}
public abstract void Add (公司 c);
public abstract void Remove (公司 c);
public abstract void Display (int depth);
public abstract void LineOfDuty();
}
class 具體公司 : 公司
{
private List<公司> children = new List<公司>();
public 具體公司 (string name) : base(name) {}
public override void Add (公司 c)
{
children.Add (c);
}
public override void Remove (公司 c)
{
children.Remove (c);
}
public override void Display (int depth)
{
Console.WriteLine (new String ('-', depth) + name);
foreach (公司 component in children)
{
component.Display (depth + 2);
}
}
// 履行職責
public override void LineOfDuty ()
{
foreach (公司 component in children)
{
component.LineOfDuty();
}
}
}
class 人力資源部 : 公司
{
public 人力資源部 (string name) : base (name) {}
public override void Add (公司 c) {}
public override void Remove (公司 c) {}
public override void Display (int depth)
{
Console.WriteLine (new String ('-', depth) + name);
}
public override void LineOfDuty()
{
Console.WriteLine ("{0} 員工招聘教育訓練管理", name);
}
}
class 財務部 : 公司
{
public 財務部 (string name) : base (name) {}
public override void Add (公司 c) {}
public override void Remove (公司 c) {}
public override void Display (int depth)
{
Console.WriteLine (new String ('-', depth) + name);
}
public override void LineOfDuty()
{
Console.WriteLine ("{0} 公司財務收支管理", name);
}
}
}
using System;
namespace DPExample
{
public class Program
{
static void Main ()
{
具體公司 root = new 具體公司("北京總公司");
root.Add (new 人力資源部("總公司人力資源部"));
root.Add (new 財務部("總公司財務部"));
具體公司 comp = new 具體公司("上海華東分公司");
comp.Add (new 人力資源部("華東分公司人力資源部"));
comp.Add (new 財務部("華東分公司財務部"));
root.Add (comp);
具體公司 comp1 = new 具體公司("南京辦事處");
comp1.Add (new 人力資源部("南京辦事處人力資源部"));
comp1.Add (new 財務部("南京辦事處財務部"));
root.Add (comp1);
具體公司 comp2 = new 具體公司("杭州辦事處");
comp2.Add (new 人力資源部("杭州辦事處人力資源部"));
comp2.Add (new 財務部("杭州辦事處財務部"));
root.Add (comp2);
Console.WriteLine ("\n結構圖:");
root.Display(1);
Console.WriteLine("\n職責:");
root.LineOfDuty();
}
}
}
留言
張貼留言