c# 组合模式

时间:2022-07-07 09:04:09 

结构图:
c# 组合模式
抽象对象:


    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();
        }
    }
 
标签:组合模式
0
投稿

猜你喜欢

  • Java面向对象编程(封装/继承/多态)实例解析

    2023-11-11 11:33:09
  • Android开发中Activity之间切换出现短暂黑屏的解决方法

    2023-02-19 04:29:38
  • 浅谈C#中的委托、事件与异步

    2022-04-25 23:05:34
  • 详解maven中央仓库连不上的解决办法

    2023-02-25 08:43:54
  • C#中的委托介绍

    2023-07-27 13:26:23
  • 消息队列-kafka消费异常问题

    2023-02-02 07:08:05
  • HashMap原理及手写实现部分区块链特征

    2023-10-15 03:27:27
  • Android Scroller的使用方法

    2023-02-03 03:57:01
  • c#根据文件类型获取相关类型图标的方法代码

    2022-07-30 10:56:41
  • c# base64转字符串实例

    2021-06-25 01:47:54
  • C# 使用Fiddler捕获本地HttpClient发出的请求操作

    2022-06-28 04:10:34
  • Maven的porfile与SpringBoot的profile结合使用案例详解

    2023-11-14 00:07:59
  • Android控件之Spinner用法实例分析

    2022-08-06 08:36:33
  • 聊一聊SpringBoot服务监控机制

    2023-02-09 02:47:48
  • 聊聊Java 中的线程中断

    2021-05-31 02:04:30
  • 使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出详解

    2023-07-18 18:15:14
  • C#数据类型转换(显式转型、隐式转型、强制转型)

    2021-11-24 13:44:25
  • 详解如何在C#中使用投影(Projection)

    2023-03-28 09:14:15
  • Java二维码登录流程实现代码(包含短地址生成,含部分代码)

    2021-10-23 02:06:26
  • Android viewpager 3D画廊的实现方法

    2021-05-28 21:19:27
  • asp之家 软件编程 m.aspxhome.com