设计模式之组合模式

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

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);
            }
        }
    }
相关推荐
霸道流氓气质24 分钟前
SpringBoot中设计模式组合使用示例-策略、模板、观察者、门面、工厂、单例。
spring boot·后端·设计模式
ttod_qzstudio2 小时前
【软考设计模式】观察者模式:一对多依赖的自动通知与订阅解耦精讲
观察者模式·设计模式
ttod_qzstudio4 小时前
【软考设计模式】命令模式:请求封装与调用接收解耦的精讲
设计模式·命令模式
春卷同学4 小时前
HarmonyOS掌上记账APP开发实践第5篇:鸿蒙 EventBus 设计模式 — MoneyTrack 跨模块通信解密
华为·设计模式·harmonyos
春卷同学6 小时前
HarmonyOS掌上记账APP开发实践第3篇:接口与实现分离 — MoneyTrack Types.ets 与 Models.ets 的双层设计模式
设计模式
NG4771 天前
【软件设计与体系结构】-策略设计模式
java·设计模式·软件工程
CaffeinePro1 天前
SOLID五大设计原则:重构优雅代码与架构的底层规范
设计模式·架构
饮茶三千1 天前
电子保函模板编辑器实战二:HTML→FTL 编译转换的设计与实现
前端·vue.js·设计模式
Kel1 天前
Node.js 本质:从全脉络视角理解运行时原理
javascript·设计模式·node.js
码哥字节1 天前
11 万 Star 的 Spec Kit,90% 的工程师只用它解决了 10% 的问题
设计模式·vibe coding·github spec kit