设计模式——组合模式

设计模式——组合模式

在这里插入图片描述
组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具体一致性

 //Component为组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component的子部件
    abstract class Component
    {
        protected string name;

        public Component(string name)
        {
            this.name = name;
        }

        public abstract void Add(Component c);//通常都用Add和Remove方法来提供增加或移除树叶或树枝的功能
        public abstract void Remove(Component c);
        public abstract void Display(int depth);
    }

    //Leaf在组合中表示叶节点对象,叶节点没有子节点
    class Leaf : Component
    {

        public Leaf(string name) : base(name)
        { 
        
        }
        public override void Add(Component c)        //由于叶子没有增加分枝和树叶,所有Add和Remove方法
        {                                            //实现它没有意义,但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口  
            Console.WriteLine("Connot add to a leaf");
        }
        public override void Remove(Component c)
        {
            Console.WriteLine("Connot Remove from a leaf");
        }


        public override void Display(int depth)//叶节点的具体方法,此次是显示其名称和级别
        {
            Console.WriteLine(new String('-',depth)+name);
        }
    }

    //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);
            }
        }
    }
        static void Main(string[] args)
        {
                    Composite root = new Composite("root");//生成树根root,根据长出两叶LeafA和LeafB
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite comp = new Composite("Composite X");//根上长出分枝Composite X,分枝上也有两叶Leaf XA和Leaf XB
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));

            root.Add(comp);

            Composite comp2 = new Composite("Composite XY");//在Composite X上再长出分枝 Composite XY分枝上也有两叶Leaf XYA和Leaf XYB
            comp2.Add(new Leaf("Leaf XYA"));
            comp2.Add(new Leaf("Leaf XYB"));

            comp.Add(comp2);

            root.Add(new Leaf("Leaf C"));

            Leaf leaf = new Leaf("Leaf D");//根部又长出两叶 Leaf C和Leaf D,可惜没长牢,被风吹走了
            root.Add(leaf);
            root.Remove(leaf);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值