• 设计模式之组合模式


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

    在这里插入图片描述

      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);
    
        }
    
        /// 
        /// Leaf在组合中表示叶节点对象,叶节点没有子节点
        /// 
        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");
            }
        }
    
    
        /// 
        /// Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加Add和删除Remove
        /// 
        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);
                }
            }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    如何使用远程控制软件并将用途最大化?4款国内外优质应用测评解析
    Spring(三)
    面试常见问题丨深入解析Spring中Bean的整个初始化过程
    dotne发布运行
    一个WPF版的Layui前端UI库
    线程池有几种创建方式?
    vue 下载的插件从哪里上传?npm发布插件详细记录
    An工具介绍之3D工具
    java:十六进制转ip地址
    【如何学习CAN总线测试】——CAN交互层测试
  • 原文地址:https://blog.csdn.net/ht_game/article/details/133084237