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




  • 相关阅读:
    -bash java: cannot execute binary file (华为鲲鹏云)
    Centos7.6编译安装数据库mysql5.7.22(华为鲲鹏云服务器案例)
    华为鲲鹏云服务器编译安装mysql-5.7.27 报错error: could not split insn
    centos7.6安装nginx并设置开机自启
    ansible常用模块实例
    Nginx、tomcat日志切割
    Linux系统文件系统损坏修复实例
    Rest模式get,put,post,delete含义与区别(转)
    从关系型数据库到非关系型数据库
    SQL Server 2012 Express LocalDB 的作用
  • 原文地址:https://www.cnblogs.com/zyizyizyi/p/3173121.html
Copyright © 2011-2022 走看看