C#设计模式-3结构型模式-3.4组合模式(Composite Pattern)

3.4.1 定义

将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性

3.4.2 场景模拟

为一家在全国许多城市都有分销机构的大公司做办公管理系统(OA),总部有人力资源、财务、运营等部门,在北京有总部,在全国几大城市设有分公司,比如上海设有华东区分部,一些省会城市设有办事处,如南京办事处,杭州办事处,总公司的人力资源部、财务部等办公管理功能在所有的分公司或办事处都需要有.

C#设计模式-3结构型模式-3.4组合模式(Composite Pattern)

3.4.3 场景代码实现

C#设计模式-3结构型模式-3.4组合模式(Composite Pattern)

公司类(抽象类或接口):

public 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);
        /// <summary>
        /// 履行职责
        /// </summary>
        public abstract void LineOfDuty();
    }

具体公司类 具体树枝类

 

  public class ConcretCompany : Company
    {
        private List<Company> children = new List<Company>();
        public ConcretCompany(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 item in children)
            {
                item.Display(depth + 2);
            }
        }

        public override void LineOfDuty()
        {
            foreach (Company item in children)
            {
                item.LineOfDuty();
            }
        }
    }

 

人力资源与财务部类,树叶节点

 public 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($"{name} 员工招聘培训管理");
        }
    }
 public 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($"{name} 公司财务收支管理");
        }
    }

客户端调用类:

 class Program
    {
        static void Main(string[] args)
        {
            ConcretCompany root = new ConcretCompany("北京总公司");
            root.Add(new HRDepartment("总公司人力资源部"));
            root.Add(new FinanceDepartment("总公司财务部"));

            ConcretCompany comp = new ConcretCompany("上海华东分公司");
            comp.Add(new HRDepartment("华东分公司人力资源部"));
            comp.Add(new FinanceDepartment("华东分公司财务部"));
            root.Add(comp);

            ConcretCompany comp1 = new ConcretCompany("南京办事处");
            comp1.Add(new HRDepartment("南京办事处人力资源部"));
            comp1.Add(new FinanceDepartment("南京办事处财务部"));
            comp.Add(comp1);

            ConcretCompany comp2 = new ConcretCompany("杭州办事处");
            comp2.Add(new HRDepartment("杭州办事处人力资源部"));
            comp2.Add(new FinanceDepartment("杭州办事处财务部"));
            comp.Add(comp2);

            Console.WriteLine("\n结构图:");
            root.Display(1);

            Console.WriteLine("\n职责:");
            root.LineOfDuty();

            Console.ReadLine();
        }
    }

调用结果:

C#设计模式-3结构型模式-3.4组合模式(Composite Pattern)

3.4.4 结构图

C#设计模式-3结构型模式-3.4组合模式(Composite Pattern)

3.4.5 通用代码

抽象类:

 public 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);
    }

具体叶子类:

 /// <summary>
    /// 叶节点
    /// </summary>
    public class Leaf : Component
    {
        public Leaf(string name) : base(name)
        {
        }

        public override void Add(Component c)
        {

        }
        public override void Remove(Component c)
        {

        }
        /// <summary>
        /// 叶节点的具体方法,这里是显示名称和级别
        /// </summary>
        /// <param name="depth"></param>
        public override void Display(int depth)
        {
            Console.WriteLine(new string('-', depth) + name);
        }
    }

子部件类,下面还有节点

/// <summary>
    /// 子部件
    /// </summary>
    public 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);
        }
        public override void Display(int depth)
        {
            Console.WriteLine(new string('-', depth) + name);
            foreach (Component compnent in children)
            {
                compnent.Display(depth + 2);
            }
        }
    }

客户端调用:

  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 comp1 = new Composite("Composite XX");
            comp1.Add(new Leaf("Leaf XXA"));
            comp1.Add(new Leaf("Leaf XXB"));
            comp.Add(comp1);

            root.Add(new Leaf("Leaf C"));
            Leaf leaf = new Leaf("Leaf D");
            root.Add(leaf);
            root.Remove(leaf);

            root.Display(1);

            Console.ReadLine();
        }
    }

调用结果:

C#设计模式-3结构型模式-3.4组合模式(Composite Pattern)

3.4.6 使用场景

当需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑组合模式

 

上一篇:Human centered design and design thinking


下一篇:以太网通信(1)UDP —— 数据发送