设计模式之组合模式

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

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);
            }
        }
    }
相关推荐
ShyanZh13 小时前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式
YYYing.21 小时前
【设计模式系列 (五) 】原型模式
开发语言·后端·设计模式·原型模式·c/c++
长按助力退休2 天前
设计模式实战,这 5 个最常用
设计模式
Carl_奕然2 天前
【智能体】Agent的四种设计模式之:Multi-Agent Collaboration
设计模式
Lost of 程序猿2 天前
建造者模式实战:告别“十参数构造函数“的数据导出任务
后端·设计模式·c#·asp.net
SL_staff2 天前
合规性折旧:当知识资产因主权缺位在审计中‘功能性清零’
java·设计模式·开源
Zane19942 天前
工厂方法一定比简单工厂高级?三种工厂模式到底该怎么选
设计模式
Hespethorn2 天前
设计模式是语言缺陷的补丁 —— 从工厂、单例、策略三个模式说起
设计模式
Lost of 程序猿2 天前
命令模式实战:把“下单后的动作“打包成可执行的任务
后端·设计模式·c#·asp.net·命令模式
一只旭宝2 天前
【C++复习】四种类型转换 + 常见设计模式 + Redis/MySQL(后端面试复习完整版)
c++·redis·笔记·mysql·设计模式