组合模式:将对象组合成树形结构以表示部门-整体的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。整体和部分被一致对待。
Component抽象类
namespace CompositePattern.CLASS { 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); } }
叶子类(Leaf)继承于 Component抽象类 透明方式重写Add()和Remove()
namespace CompositePattern.CLASS { class Leaf:Component { public Leaf(string name) : base(name) { } public override void Add(Component c) { } public override void Remove(Component c) { } public override void Display(int depth) { Console.WriteLine(new String('-',depth) + name); } } }
Composite类 继承于 Component抽象类, 存储子部件包括叶和枝 <递归遍历树形结构>
namespace CompositePattern.CLASS { class Composite:Component { public Composite(string name) : base(name) { } private List<Component> children = new List<Component>(); 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 com in children) { com.Display(depth + 1); } } } }
测试类(TestMain)
namespace CompositePattern { class TestMain { static void Main(string[] args) { Composite root = new Composite("root"); root.Add(new Leaf("LeafA")); root.Add(new Leaf("LeafB")); Composite com = new Composite("Branch"); com.Add(new Leaf("BranchLeafA")); com.Add(new Leaf("BranchLeafB")); root.Add(com); Composite com1 = new Composite("Branch1"); com1.Add(new Leaf("Branch1A")); com1.Add(new Leaf("Branch1B")); root.Add(com1); Leaf Leaf = new Leaf("LeafC"); root.Add(Leaf); root.Remove(Leaf); root.Display(1); Console.WriteLine(); } } }
测试结果:
以下以公司结构为例,实现各个财务部和人力资源部的职责和结构
公司抽象类(Company)
namespace CompositePatternTest.CLASS { 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 Duty(); //履行职责 } }
财务部类(HRDepartment)继承于公司抽象类(Company)
namespace CompositePatternTest.CLASS { class HRDepartment:Company { public HRDepartment(string name) : base(name) { } public override void Add(Company c) { } public override void Remove(Company c) { } public override void Display(int depth) { Console.WriteLine(new String('*',depth) + name); } public override void Duty() { Console.WriteLine("{0}人力资源部负责培训员工!",name); } } }
人力资源部类(FinanceDepartment)继承于公司抽象类(Company)
namespace CompositePatternTest.CLASS { class FinanceDepartment:Company { public FinanceDepartment(string name) : base(name) { } public override void Add(Company c) { } public override void Remove(Company c) { } public override void Display(int depth) { Console.WriteLine(new String('*',depth) + name); } public override void Duty() { Console.WriteLine("{0}财务部负责公司财务管理!",name); } } }
具体公司(CreateCompany)继承于公司抽象类(Company)
namespace CompositePatternTest.CLASS { class CreateCompany:Company { public CreateCompany(string name) : base(name) { } private List<Company> children = new List<Company>(); public override void Add(Company c) { children.Add(c); } public override void Remove(Company c) { children.Remove(c); } public override void Display(int depth) { Console.WriteLine(new String('*',depth) + name); foreach(Company com in children) { com.Display(depth + 1); } } public override void Duty() { foreach(Company com in children) { com.Duty(); } } } }
测试类(TestMain)
namespace CompositePatternTest { class TestMain { static void Main(string[] args) { CreateCompany root = new CreateCompany("北京总公司"); root.Add(new HRDepartment("总公司人力资源部")); root.Add(new FinanceDepartment("总公司财务部")); CreateCompany branch = new CreateCompany("上海华东分公司"); branch.Add(new HRDepartment("上海华东分公司人力资源部")); branch.Add(new FinanceDepartment("上海华东分公司财务部")); root.Add(branch); CreateCompany branch1 = new CreateCompany("南京办事处"); branch1.Add(new HRDepartment("南京办事处人力资源部")); branch1.Add(new FinanceDepartment("南京办事处财务部")); branch.Add(branch1); CreateCompany branch2 = new CreateCompany("杭州办事处"); branch2.Add(new HRDepartment("杭州办事处人力资源部")); branch2.Add(new FinanceDepartment("杭州办事处财务部")); branch.Add(branch2); Console.WriteLine("结构图"); root.Display(1); Console.WriteLine("职责"); root.Duty(); Console.ReadLine(); } } }
测试结果:
要么忍,要么狠,要么滚!