设计模式之组合模式

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

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);
            }
        }
    }
相关推荐
wyzqhhhh10 小时前
前端常见的设计模式
前端·设计模式
m0_7482336411 小时前
C++开发中的常用设计模式:深入解析与应用场景
javascript·c++·设计模式
Wind哥12 小时前
设计模式23种-C++实现
开发语言·c++·windows·设计模式
闲人编程13 小时前
Python设计模式实战:用Pythonic的方式实现单例、工厂模式
开发语言·python·单例模式·设计模式·工厂模式·codecapsule·pythonic
czy878747517 小时前
用C语言实现组合模式
c语言·组合模式
御承扬1 天前
编程素养提升之EffectivePython(Builder篇)
python·设计模式·1024程序员节
杯莫停丶1 天前
设计模式之:享元模式
java·设计模式·享元模式
杯莫停丶1 天前
设计模式之:组合模式
设计模式·组合模式
Hero | 柒1 天前
设计模式之建造者模式
java·设计模式·1024程序员节
周杰伦_Jay1 天前
【常用设计模式全解析】创建型模式(聚焦对象创建机制)、结构型模式(优化类与对象的组合关系)、行为型模式(规范对象间的交互行为)
设计模式·架构·开源·交互·1024程序员节