设计模式之组合模式

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

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);
            }
        }
    }
相关推荐
明洞日记3 小时前
【设计模式手册007】原型模式 - 通过复制创建对象的艺术
java·设计模式·原型模式
u***j3246 小时前
算法设计模式总结
算法·设计模式
烤麻辣烫6 小时前
23种设计模式(新手)-7迪米特原则 合成复用原则
java·开发语言·学习·设计模式·intellij-idea
G***66917 小时前
算法设计模式:贪心与动态规划
算法·设计模式·动态规划
努力的光头强7 小时前
《智能体设计模式》从零基础入门到精通,看这一篇就够了!
大数据·人工智能·深度学习·microsoft·机器学习·设计模式·ai
top_designer1 天前
Substance 3D Stager:电商“虚拟摄影”工作流
人工智能·3d·设计模式·prompt·技术美术·教育电商·游戏美术
lapiii3581 天前
[智能体设计模式] 第11章:目标设定与监控模式
人工智能·设计模式
在未来等你1 天前
AI Agent设计模式 Day 7:Tree-of-Thoughts模式:树形思维探索
设计模式·llm·react·ai agent·plan-and-execute
烤麻辣烫1 天前
23种设计模式(新手)-5里氏替换原则
java·学习·设计模式·intellij-idea·里氏替换原则
g***B7381 天前
前端组件设计模式,复用与扩展
前端·设计模式