zoukankan      html  css  js  c++  java
  • 组合模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Composite
    {
        public abstract class Component
        {
            public abstract void Show();
            //添加部件
            public abstract void Add(Component component);
            //删除部件
            public abstract void Remove(Component component);
            public string Name { getset; }
        }
        public class Leaf : Component 
        {
            public override void Add(Component component) 
            {
                throw new NotImplementedException(); 
            }
            public override void Remove(Component component)
            {
                throw new NotImplementedException();
            }
            public override void Show() 
            {
                Console.WriteLine(Name);
            }
        }
        public class Node : Component 
        {
            private List<Component> myChildren = new List<Component>();
            public override void Add(Component component)
            {
                myChildren.Add(component);
            }
            public override void Remove(Component component)
            {
                myChildren.Remove(component);
            }
            public override void Show() 
            {
                Console.WriteLine(Name);
                foreach (Component child in myChildren) 
                {
                    child.Show();
                }
            }
        }
    }
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Composite
    {
        class Program
        {
            static void Main(string[] args)
            {
                //构造根节点
                Node rootComponent = new Node();
                rootComponent.Name = "根节点";
                //添加两个叶子几点,也就是子部件
                Leaf l = new Leaf();
                l.Name = "叶子节点一";
                Leaf l1 = new Leaf();
                l1.Name = "叶子节点二";
                rootComponent.Add(l);
                rootComponent.Add(l1);
                rootComponent.Show();
                Console.ReadLine();
            }
        }
    }




  • 相关阅读:
    深入学习SlidingMenu 2015-06-12 20:27 856人阅读 评论(0) 收藏
    Android 判断SD卡是否存在及容量查询
    第三方登录,授权,分享
    GLSurfaceView用法详解
    Java/android面试题
    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
    填充数字以达到位数
    web api post
    .net测试方法效率获取系统当前时间
    vs2012更新问题
  • 原文地址:https://www.cnblogs.com/zyizyizyi/p/3173121.html
Copyright © 2011-2022 走看看