设计模式之组合模式

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

csharp 复制代码
  class Program
    {
        static void Main(string[] args)
        {
            Composite root = new Composite("root");
            root.Add(new Left("Left A"));
            root.Add(new Left("Left B"));

            Composite comp = new Composite("Composite X");
            comp.Add(new Left("Left XA"));
            comp.Add(new Left("Left XB"));

            root.Add(comp);

            Composite comp2 = new Composite("Composite XY");
            comp2.Add(new Left("Left XYA"));
            comp2.Add(new Left("Left XYB"));

            comp.Add(comp2);

            root.Add(new Left("Left C"));

            Left leaf = new Left("Left D");

            root.Add(leaf);
            root.Remove(leaf);

            root.Display(1);

            Console.ReadLine();
        }
    }

    //Component为组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component的子部件
    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>
    /// Leaf在组合中表示叶节点对象,叶节点没有子节点
    /// </summary>
    class Left : Component
    {
        public Left(string name) : base(name)
        {
            
        }

        public override void Add(Component c)//由于叶子没有再增加分枝和树叶,所以Add和Remove方法实现它没有意义,但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具有完全一致的接口
        {
            Console.WriteLine("Cannot add to leaf");
        }

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

        public override void Remove(Component c)
        {
            Console.WriteLine("Cannot remove from a leaf");
        }
    }


    /// <summary>
    /// Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加Add和删除Remove
    /// </summary>
    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 component in children)
            {
                component.Display(depth + 2);
            }
        }
    }
相关推荐
Aeside136 分钟前
揭秘 Nginx 百万并发基石:Reactor 架构与 Epoll 底层原理
后端·设计模式
帅气的你2 小时前
从零封装一个通用的 API 接口返回类:统一前后端交互格式
java·设计模式
阿里巴巴淘系技术团队官网博客2 小时前
GenAI输出内容控制的5种设计模式
设计模式
沛沛老爹4 小时前
Skills高级设计模式(一):向导式工作流与模板生成
java·人工智能·设计模式·prompt·aigc·agent·web转型
__万波__6 小时前
二十三种设计模式(二十二)--策略模式
java·设计模式·策略模式
Overt0p7 小时前
抽奖系统(6)
java·spring boot·redis·设计模式·rabbitmq·状态模式
__万波__7 小时前
二十三种设计模式(二十三)--责任链模式
java·设计模式·责任链模式
帅次8 小时前
系统设计方法论全解:原则、模型与用户体验核心要义
设计模式·流程图·软件工程·软件构建·需求分析·设计规范·规格说明书
蔺太微8 小时前
装饰器模式(Decorator Pattern)
设计模式·装饰器模式
reddingtons16 小时前
【游戏宣发】PS “生成式扩展”流,30秒无损适配全渠道KV
游戏·设计模式·新媒体运营·prompt·aigc·教育电商·游戏美术