设计模式之--组合模式
组合模式:是将对象组合成树形结构一表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。
组合模式示例代码如下:
class Program { static void Main(string[] args) { Composite root = new Composite("root"); root.Add(new Left("Left A")); root.Add(new Left("Left B")); Composite comp = new Composite("Composite X"); comp.Add(new Left("Left xA")); comp.Add(new Left("Left xB")); root.Add(comp); Composite comp2 = new Composite("Composite Y"); root.Add(new Left("Left yA")); root.Add(new Left("Left yB")); comp.Add(comp2); root.Display(1); Console.Read(); } } 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 Left:Component { public Left(string name):base(name) { } public override void Add(Component c) { Console.WriteLine("Cannot add to a left"); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void Remove(Component c) { Console.WriteLine("Cannot remove from a left"); } } class Composite : Component { private List<Component> childComponet = new List<Component>(); public Composite(string name):base(name) { } public override void Add(Component c) { childComponet.Add(c); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); foreach(Component component in childComponet) { component.Display(depth + 2); } } public override void Remove(Component c) { childComponet.Remove(c); } }
何时使用组合模式:
需求中体现部分与整体层次的结构时,以及希望用户可以忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时,就应该考虑使用组合模式。
Component:组合中的对象声明接口,在适当情况下,实现所有类共有的默认行为。声明一个接口用于访问和管理Component的子部件。
Composite:定义由枝节点行为,用来存储子部件,在 Component接口中实现与子部件有关的操作,比如增加Add和删除Remove。
Left:在组合中表示叶节点对象,叶节点没有子节点。
实例
abstract class Company { protected string name; public Company(string name) { this.name = name; } public abstract void Add(Company C); public abstract void Remove(Company c); public abstract void Display(int depth); public abstract void LineOfDuty();//履行职责 } class ConcreteCompany:Company { private List<Company> children = new List<Company>(); public ConcreteCompany(string name):base(name) { } public override void Add(Company c) { children.Add(c); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); foreach(Company com in children) { com.Display(depth + 2); } } public override void LineOfDuty() { foreach(Company com in children) { com.LineOfDuty(); } } public override void Remove(Company c) { children.Remove(c); } } class HRDepartment : Company { public override void Add(Company C) { throw new NotImplementedException(); } public HRDepartment(string name):base(name) { } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void LineOfDuty() { Console.WriteLine("{0} 员工招聘培训管理", name); } public override void Remove(Company c) { throw new NotImplementedException(); } } class FinanceDepartment:Company { public FinanceDepartment(string name):base(name) { } public override void Add(Company C) { throw new NotImplementedException(); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void LineOfDuty() { Console.WriteLine("{0} 公司财务收支管理", name); } public override void Remove(Company c) { throw new NotImplementedException(); } }
ConcreteCompany root = new ConcreteCompany("北京总部"); root.Add(new HRDepartment("总公司人力资源部")); root.Add(new FinanceDepartment("总公司财务部")); ConcreteCompany root2 = new ConcreteCompany("上海华东分公司"); root2.Add(new HRDepartment("华东分公司人力资源部")); root2.Add(new FinanceDepartment("华东分公司财务部")); root.Add(root2); root.Display(1); Console.Read();