Core Design Patterns(4) Composite 组合模式
VS 2008
组合模式使得我们可以以同样的方式看待单一Comonent和Component集合。
也就是,单一对象和该对象的组合具有相同的抽象行为。
组合模式通常用于构建树型结构。
1. 模式UML图
2. 应用
项目中有一组部门信息,部门之间具有父子关系,是一个典型的树型结构,如有一个部门叫市政局,下属有市管处、公路处两个部门,公路处下属又有公路署部门等等。
Department.cs
IDepartment.cs
DepartmentLeaf.cs
DepartmentComposite.cs
Client
Output
3. 思考
对于组合模式,通常可以将前述的UML进行退还,我们甚至可以不要Leaf类,为了灵活扩展性,每个叶子都可以演变为树枝。
对于本例的部门树型结构其实只是选择了一个最简单的行为进行实现,真正的树,需要实现的行为还远不止这些。
组合模式使得我们可以以同样的方式看待单一Comonent和Component集合。
也就是,单一对象和该对象的组合具有相同的抽象行为。
组合模式通常用于构建树型结构。
1. 模式UML图
2. 应用
项目中有一组部门信息,部门之间具有父子关系,是一个典型的树型结构,如有一个部门叫市政局,下属有市管处、公路处两个部门,公路处下属又有公路署部门等等。
Department.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public class Department {
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public class Department {
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
}
IDepartment.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public interface IDepartment {
void Add(IDepartment department);
void Remove(IDepartment department);
int GetChildCount();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public interface IDepartment {
void Add(IDepartment department);
void Remove(IDepartment department);
int GetChildCount();
}
}
DepartmentLeaf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public class DepartmentLeaf : IDepartment {
private Department department;
public Department Department {
get { return this.department; }
}
public DepartmentLeaf(Department department) {
this.department = department;
}
IDepartment Members
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public class DepartmentLeaf : IDepartment {
private Department department;
public Department Department {
get { return this.department; }
}
public DepartmentLeaf(Department department) {
this.department = department;
}
IDepartment Members
}
}
DepartmentComposite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public class DepartmentComposite : IDepartment {
private Department department;
private List<IDepartment> list = new List<IDepartment>();
public Department Department {
get { return this.department; }
}
public DepartmentComposite(Department department) {
this.department = department;
}
IDepartment Members
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern.Composite.BLL {
public class DepartmentComposite : IDepartment {
private Department department;
private List<IDepartment> list = new List<IDepartment>();
public Department Department {
get { return this.department; }
}
public DepartmentComposite(Department department) {
this.department = department;
}
IDepartment Members
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Composite.BLL;
namespace DesignPattern.Composite {
class Program {
static void Main(string[] args) {
IDepartment j1 = new DepartmentComposite(new Department() { DepartmentId=1, DepartmentName="市政局" });
IDepartment c1 = new DepartmentComposite(new Department() { DepartmentId = 2, DepartmentName = "市管处" });
IDepartment c2 = new DepartmentComposite(new Department() { DepartmentId = 3, DepartmentName = "公路处" });
IDepartment s1 = new DepartmentLeaf(new Department() { DepartmentId = 4, DepartmentName = "公路署" });
c2.Add(s1);
j1.Add(c1);
j1.Add(c2);
Console.WriteLine(j1.GetChildCount().ToString());
Console.WriteLine(c1.GetChildCount().ToString());
Console.WriteLine(c2.GetChildCount().ToString());
Console.WriteLine(s1.GetChildCount().ToString());
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Composite.BLL;
namespace DesignPattern.Composite {
class Program {
static void Main(string[] args) {
IDepartment j1 = new DepartmentComposite(new Department() { DepartmentId=1, DepartmentName="市政局" });
IDepartment c1 = new DepartmentComposite(new Department() { DepartmentId = 2, DepartmentName = "市管处" });
IDepartment c2 = new DepartmentComposite(new Department() { DepartmentId = 3, DepartmentName = "公路处" });
IDepartment s1 = new DepartmentLeaf(new Department() { DepartmentId = 4, DepartmentName = "公路署" });
c2.Add(s1);
j1.Add(c1);
j1.Add(c2);
Console.WriteLine(j1.GetChildCount().ToString());
Console.WriteLine(c1.GetChildCount().ToString());
Console.WriteLine(c2.GetChildCount().ToString());
Console.WriteLine(s1.GetChildCount().ToString());
}
}
}
Output
3. 思考
对于组合模式,通常可以将前述的UML进行退还,我们甚至可以不要Leaf类,为了灵活扩展性,每个叶子都可以演变为树枝。
对于本例的部门树型结构其实只是选择了一个最简单的行为进行实现,真正的树,需要实现的行为还远不止这些。