设计模式之组合模式

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

Created with Raphaël 2.1.0 Leaf Leaf Component Component Composite Composite 继承 继承对方并且与父类呈聚合关系

抽象类:
Component: 方法: Add(Component c) Remove(Component c) Display(int depth)

子类:
Leaf: Display(int depth)
子类(具体类)
Composite: Add(Component c) Remove(Component c) Display(int depth)

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

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

        }

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

    //枝节点
    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 item in children)
            {
                item.Display(depth+2);
            }
        }
    }

Main方法调用:

  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"));
            root.Add(comp2);

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

            Console.ReadLine();
        }

运行结果:
这里写图片描述

这种方式叫做透明模式,也就是说在Component中声明所有用来管理子对象的方法,其中包括Add、Remove等,这样实现Component的所有子类都具备了Add和Remove,这样做的好处就是叶节点和枝节点对于外界没有区别, 它们具备完全一致的行为,但问题也很明显,因为Leaf本身不具备Add()和Remove()的功能,所以实现它是无意义的

接下来,在练习一道题:
一个公司会有多个分公司,同时分公司和总公司都会有财务和人事部门。 它们具有相同的情况。

Created with Raphaël 2.1.0 财务部 财务部 公司 公司 人事部 人事部 具体公司 具体公司 继承 继承 继承和聚合关系

个人建议,UML图再做题。实在是想不到的话可以看看下面的代码。或者自己做完后比较一番。这样可以加深印象。
提示: 抽象类公司 中包含的方法: 增加(公司 ) 减少(公司)
显示(int) 履行职责()

  //抽象工厂类
    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 Show(int dept);

        public abstract void LineOfDuty();
    }

    //具体公司
    public class ConcreteCompany:Company 
    {
        private List<Company> list = new List<Company>();
        public ConcreteCompany(string name) : base(name) 
        {
        }

        public override void Add(Company c)
        {
            list.Add(c);
        }
        public override void Remove(Company c)
        {
            list.Remove(c);
        }

        public override void Show(int dept)
        {
            Console.WriteLine(new String('-',dept)+name);
            foreach (var item in list)
            {
                item.Show(dept+2);
            }
        }

        public override void LineOfDuty()
        {
            foreach (var item in list)
            {
                item.LineOfDuty();
            }
        }
    }

    //财务部
    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 Show(int dept)
        {
            Console.WriteLine(new String('-', dept)+name);
        }
        public override void LineOfDuty()
        {
            Console.WriteLine("我是{0}的,我负责发工资",name);
        }

    }
    //人事部门
    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 Show(int dept)
        {
            Console.WriteLine(new String('-', dept)+name);
        }
        public override void LineOfDuty()
        {
            Console.WriteLine("我是{0}的,我负责招聘新员工",name);
        }
    }
 static void Main(string[] args)
        {
            ConcreteCompany c = new ConcreteCompany("总公司");
            c.Add(new HRDepartment("北京总公司人事部"));
            c.Add(new FinanceDepartment("北京总公司财务部"));

            ConcreteCompany c1 = new ConcreteCompany("上海分公司");
            c1.Add(new HRDepartment("上海分公司人事部"));
            c1.Add(new FinanceDepartment("上海分公司财务部"));
            c.Add(c1);
            ConcreteCompany c2 = new ConcreteCompany("杭州分公司");
            c2.Add(new HRDepartment("杭州分公司人事部"));
            c2.Add(new FinanceDepartment("杭州分公司财务部"));
            c.Add(c2);

            c.Show(1);
            c.LineOfDuty();
            Console.ReadLine();
        }

结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值