zoukankan      html  css  js  c++  java
  • 组合模式

    组合模式:将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。需求中式体现部分与整体层次的结构时,统一地使用组合对象中的所有对象时,应该考虑使用组合模式。

    结构图:


     

     抽象对象:

        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 Leaf : Component
        {
            public Leaf(string name)
                : base(name)
            { }
            public override void Add(Component c)
            {
                //throw new NotImplementedException();
                Console.WriteLine("Cannot add to a Leaf");
            }
            public override void Remove(Component c)
            {
                //throw new NotImplementedException();
                Console.WriteLine("Cannot remove to a Leaf");
            }
            public override void Display(int depth)
            {
                //throw new NotImplementedException();
                Console.WriteLine(new string('-', depth) + name);
            }
        }

    可以有子结点:

        class Composite : Component
        {
            private List<Component> children = new List<Component>();
            public Composite(string name)
                : base(name)
            { }
            public override void Add(Component c)
            {
                //throw new NotImplementedException();
                children.Add(c);
            }
            public override void Remove(Component c)
            {
                //throw new NotImplementedException();
                children.Remove(c);
            }
            public override void Display(int depth)
            {
                //throw new NotImplementedException();
                Console.WriteLine(new string('-', depth) + name);
                foreach (Component component in children)
                {
                    component.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 comp2 = new Composite("Composite X");
                comp2.Add(new Leaf("Leaf XYA"));
                comp2.Add(new Leaf("Leaf XYB"));
                comp.Add(comp2);

                root.Display(1);

                Console.ReadKey();
            }
        }
     
  • 相关阅读:
    Python + unittest + HTMLTestRunnerCN 生成接口自动化测试报告
    python 读写操作CSV文件
    with关键字
    Django常规命令大全
    科技阅读与写作资料
    Topics in Service Computing
    学习总结100515
    【论文收集】PQDT硕博库中的搜索结果service composition
    毕业开题结束感想
    excle操作备忘
  • 原文地址:https://www.cnblogs.com/lmfeng/p/2621038.html
Copyright © 2011-2022 走看看