码迷,mamicode.com
首页 > 其他好文 > 详细

笔记-大话设计模式-19 组合模式

时间:2015-09-09 01:00:58      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

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

Demo1:

    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 Composite : Component
    {
        private IList<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 (var component in children)
            {
                component.Display(depth + 2);
            }
        }
    }
    class Leaf : Component
    {
        public Leaf(string name)
            : base(name)
        {

        }

        public override void Add(Component c)
        {
            throw new NotImplementedException();
        }

        public override void Remove(Component c)
        {
            throw new NotImplementedException();
        }

        public override void Display(int depth)
        {
            Console.WriteLine(new string(-, depth) + name);
        }
    }

Test:

            Composite root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite firstLevel = new Composite("第一级");
            firstLevel.Add(new Leaf("第一级的叶子"));

            root.Add(firstLevel);

            Composite secondLevel = new Composite("第二级");
            secondLevel.Add(new Leaf("第二级的叶子"));

            firstLevel.Add(secondLevel);

            root.Display(1);

技术分享

Demo2:

    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 Dutys();
    }
    class ConcreteCompany : Company
    {
        private IList<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 (var item in children)
            {
                item.Display(depth + 2);
            }
        }

        public override void Dutys()
        {
            foreach (var item in children)
            {
                item.Dutys();
            }
        }
    }
    class ADepartment : Company
    {
        public ADepartment(string name)
            : base(name)
        {

        }

        public override void Add(Company c)
        {
            throw new NotImplementedException();
        }

        public override void Remove(Company c)
        {
            throw new NotImplementedException();
        }

        public override void Display(int depth)
        {
            Console.WriteLine(new string(-, depth) + name);
        }

        public override void Dutys()
        {
            Console.WriteLine("{0} 的ADepartment履行职责", name);
        }
    }
    class BDepartment : Company
    {
        public BDepartment(string name)
            : base(name)
        {

        }

        public override void Add(Company c)
        {
            throw new NotImplementedException();
        }

        public override void Remove(Company c)
        {
            throw new NotImplementedException();
        }

        public override void Display(int depth)
        {
            Console.WriteLine(new string(-, depth) + name);
        }

        public override void Dutys()
        {
            Console.WriteLine("{0} 的BDepartment履行职责", name);
        }
    }

 

Test:

            ConcreteCompany root = new ConcreteCompany("北京总公司");
            root.Add(new ADepartment("总公司A部门"));
            root.Add(new BDepartment("总公司B部门"));

            ConcreteCompany branch1 = new ConcreteCompany("华东分公司");
            branch1.Add(new ADepartment("分公司的A部门"));
            branch1.Add(new BDepartment("分公司的B部门"));

            root.Add(branch1);

            root.Display(1);

            Console.WriteLine(Environment.NewLine);

            root.Dutys();

技术分享

 

笔记-大话设计模式-19 组合模式

标签:

原文地址:http://www.cnblogs.com/laixiancai/p/4793435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!