組合模式 - Composite Pattern
用途
將對象”組合”成樹結構以表示部分-整體層次結構
classDiagram
Client ..> Leaf
Client ..> Composite
Leaf --|> Component
Composite --|> Component
Composite "1" o--> "0..*" Component
namespace CompositePattern {
class Component{
<<Abstract>>
+Operation()*
}
class Leaf{
+Operation()
}
class Composite{
-List<Component> members
+Operation()
+Add(Component)
+Remove(Component)
+GetChild()
}
}
class Client{
+Operation()
}
例子
當談到組合模式(Composite Pattern)時,我們可以以一個組織架構為例來解釋
想像一個組織,它由多個部門組成,每個部門又可以進一步分為多個小組或子部門,而這些小組或子部門又可以進一步包含更小的單位或成員
整個組織結構形成了一個階層結構
在組織架構中,每個部門或單位都具有相似的屬性和行為,無論是部門還是小組,都有負責特定任務的成員
這種階層結構和相似的屬性和行為可以使用組合模式來設計和實現
Component
1
2
3
4
5
6
7
8
9
10
11
12
// 組織成員抽象類別
public abstract class OrganizationMember
{
protected string name;
public OrganizationMember(string name)
{
this.name = name;
}
public abstract void DisplayMemberInfo();
}
Leaf
1
2
3
4
5
6
7
8
9
10
11
12
// 部門類別,同時也是葉子節點
public class Department : OrganizationMember
{
public Department(string name) : base(name)
{
}
public override void DisplayMemberInfo()
{
Console.WriteLine("Department: " + name);
}
}
Composite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 組織類別,同時也是容器節點
public class Organization : OrganizationMember
{
private List<OrganizationMember> members = new List<OrganizationMember>();
public Organization(string name) : base(name)
{
}
public void AddMember(OrganizationMember member)
{
members.Add(member);
}
public void RemoveMember(OrganizationMember member)
{
members.Remove(member);
}
public override void DisplayMemberInfo()
{
Console.WriteLine("Organization: " + name);
foreach (var member in members)
{
member.DisplayMemberInfo();
}
}
}
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 建立組織架構
Organization organization = new Organization("ABC Company");
Department salesDepartment = new Department("Sales Department");
Department marketingDepartment = new Department("Marketing Department");
Department developmentDepartment = new Department("Development Department");
Department qaDepartment = new Department("QA Department");
Department customerServiceDepartment = new Department("Customer Service Department");
organization.AddMember(salesDepartment);
organization.AddMember(marketingDepartment);
organization.AddMember(developmentDepartment);
organization.AddMember(qaDepartment);
organization.AddMember(customerServiceDepartment);
// 顯示組織架構
organization.DisplayMemberInfo();
Console.ReadLine();
延伸
本文章以 CC BY 4.0 授權