• 设计模式之组合模式


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

    在这里插入图片描述

      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
  • 相关阅读:
    ant vue2.0 开始时间 结束时间联动
    【微信小程序】WXSS和全局、页面配置入门砖
    【音视频基础】音频基础理论
    webpack5基础--03_开发模式介绍
    Qt超时自动关闭子窗口
    VMware虚拟机中的Linux通过NAT模式共享主机网卡实现与外部设备通信
    Python实现邮件发送时如何设置服务器参数?
    Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
    计算机毕业设计(附源码)python智能答疑系统app
    从单例谈double-check必要性,多种单例各取所需
  • 原文地址:https://blog.csdn.net/ht_game/article/details/133084237