重要概念
1. 将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
2. 透明方式与安全方式
透明方式:抽象层接口最大化
安全方式:抽象层接口最小化
3.需求中是体现部分和整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。
4. 组合模式的核心技巧是,模拟一个数型结构,其中的元素具有相同的抽象,抽象中拥有添加和删除子节点的功能,每个节点本身要维护一个子节点的列表,有了这个结构之后就可以维护一组数型结构。
5. 组合模式可以无限极的组合对象,具有相同接口的抽象。客户无需去判断叶子节点和树干节点,因为叶子节点只需不实现添加和移除子节点的功能即可。
6.
7.
示例代码
using System; using System.Collections.Generic; using System.Text; namespace 组合模式 { class Program { static void Main(string[] args) { 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); Console.Read(); } } /// <summary> /// 抽象类 /// </summary> abstract class Component { protected string name; public Component(string name) { this.name = name; } //通过Add和REmove方法提供增加和删除树节点的功能 public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth); } class Composite : Component { //这个子对象集合用来存储其下的节点 private List<Component> children = new List<Component>(); public Composite(string name) : base(name) { } public override void Add(Component c) { children.Add(c); } public override void Remove(Component c) { children.Remove(c); } /// <summary> /// 显示自己,也显示其下的子节点遍历 /// </summary> /// <param name="depth"></param> public override void Display(int depth) { Console.WriteLine(new String('-', depth) + name); foreach (Component component in children) { component.Display(depth + 2); } } } class Leaf : Component { public Leaf(string name) : base(name) { } /// <summary> /// Leaf中没有子节点,实现添加和移除是没有意义的,但是继承统一的接口保持一致。 /// </summary> /// <param name="c"></param> 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); } } }
公司管理系统
using System; using System.Collections.Generic; using System.Text; namespace 组合模式 { class Program { static void Main(string[] args) { ConcreteCompany root = new ConcreteCompany("北京总公司"); root.Add(new HRDepartment("总公司人力资源部")); root.Add(new FinanceDepartment("总公司财务部")); ConcreteCompany comp = new ConcreteCompany("上海华东分公司"); comp.Add(new HRDepartment("华东分公司人力资源部")); comp.Add(new FinanceDepartment("华东分公司财务部")); root.Add(comp); ConcreteCompany comp1 = new ConcreteCompany("南京办事处"); comp1.Add(new HRDepartment("南京办事处人力资源部")); comp1.Add(new FinanceDepartment("南京办事处财务部")); comp.Add(comp1); ConcreteCompany comp2 = new ConcreteCompany("杭州办事处"); comp2.Add(new HRDepartment("杭州办事处人力资源部")); comp2.Add(new FinanceDepartment("杭州办事处财务部")); comp.Add(comp2); Console.WriteLine("\n结构图:"); root.Display(1); Console.WriteLine("\n职责:"); root.LineOfDuty(); Console.Read(); } } 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 Remove(Company c) { children.Remove(c); } public override void Display(int depth) { Console.WriteLine(new String('-', depth) + name); foreach (Company component in children) { component.Display(depth + 2); } } //履行职责 public override void LineOfDuty() { foreach (Company component in children) { component.LineOfDuty(); } } } //人力资源部 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 LineOfDuty() { Console.WriteLine("{0} 员工招聘培训管理", name); } } //财务部 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 LineOfDuty() { Console.WriteLine("{0} 公司财务收支管理", name); } } }
冯瑞涛