c# 组合模式

 更新时间:2020年6月25日 11:43  点击:1306
结构图:

抽象对象:
复制代码 代码如下:

    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);
    }

无子节点的:
复制代码 代码如下:

    class Leaf : Component
    {
        public Leaf(string name)
            : base(name)
        { }
        public override void Add(Component c)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Cannot add to a Leaf");
        }
        public override void Remove(Component c)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Cannot remove to a Leaf");
        }
        public override void Display(int depth)
        {
            //throw new NotImplementedException();
            Console.WriteLine(new string('-', depth) + name);
        }
    }

可以有子结点:
复制代码 代码如下:

    class Composite : Component
    {
        private List<Component> children = new List<Component>();
        public Composite(string name)
            : base(name)
        { }
        public override void Add(Component c)
        {
            //throw new NotImplementedException();
            children.Add(c);
        }
        public override void Remove(Component c)
        {
            //throw new NotImplementedException();
            children.Remove(c);
        }
        public override void Display(int depth)
        {
            //throw new NotImplementedException();
            Console.WriteLine(new string('-', depth) + name);
            foreach (Component component in children)
            {
                component.Display(depth + 2);
            }
        }
    }

 主函数调用:
复制代码 代码如下:

    class Program
    {
        static void Main(string[] args)
        {
            Composite root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));
            Composite comp = new Composite("Composite X");
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));
            root.Add(comp);
            Composite comp2 = new Composite("Composite X");
            comp2.Add(new Leaf("Leaf XYA"));
            comp2.Add(new Leaf("Leaf XYB"));
            comp.Add(comp2);
            root.Display(1);
            Console.ReadKey();
        }
    }
 
[!--infotagslink--]

相关文章

  • C# 设计模式系列教程-组合模式

    组合模式可以使客户端调用简单,它可以一致使用组合结构或是其中单个对象,简化了客户端代码。...2020-06-25
  • 详细总结Java组合模式

    今天带大家了解Java设计模式中的组合模式,下文中对组合模式介绍的非常详细,还有相关代码,对正在学习Java的小伙伴们很有帮助,需要的朋友可以参考下...2021-05-15
  • 深入剖析设计模式中的组合模式应用及在C++中的实现

    这篇文章主要介绍了设计模式中的组合模式应用及在C++中的实现,组合模式可以清晰地反映出递归构建树状的组合结构,需要的朋友可以参考下...2020-04-25
  • C++设计模式之组合模式

    这篇文章主要介绍了C++设计模式之组合模式,本文讲解什么是组合模式、组合模式的优点、组合模式实例等内容,需要的朋友可以参考下...2020-04-25
  • 浅谈PHP面向对象之访问者模式+组合模式

    下面小编就为大家带来一篇浅谈PHP面向对象之访问者模式+组合模式。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-05-26
  • asp.net 组合模式的一个例子

    asp.net 组合模式的一个例子,方便学习asp.net的朋友作为参考...2021-09-22
  • java实现Composite组合模式的实例代码

    这篇文章主要介绍了java实现Composite组合模式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-22
  • 剖析设计模式编程中C#对于组合模式的运用

    这篇文章主要介绍了设计模式编程中C#对于组合模式的运用,理论上来说组合模式包含抽象构件、树叶构件和树枝构件三个角色,需要的朋友可以参考下...2020-06-25
  • C#组合模式实例详解

    这篇文章主要介绍了C#组合模式,实例分析了C#实现组合模式的原理与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C++设计模式之组合模式(Composite)

    这篇文章主要为大家详细介绍了C++设计模式之组合模式Composite,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • c# 组合模式

    组合模式:将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。需求中式体现部分与整体层次的结构时,统一地使用组合对象中的所有对象时,应该考虑使用组合模式...2020-06-25
  • Java设计模式之组合模式的示例详解

    组合模式,又叫部分整体模式,它创建了对象组的数据结构组合模式使得用户对单个对象和组合对象的访问具有一致性。本文将通过示例为大家详细介绍一下组合模式,需要的可以参考一下...2022-03-01