惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
小众软件
小众软件
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
B
Blog
量子位
B
Blog RSS Feed
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
Jina AI
Jina AI
C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG

博客园 - zyi

Windows FTP安装 Oracle优化 Oracle的汉字转拼音首字母的函数 创建job 建索引 加解密 代码规范工具 耦合内聚封装 Dev进度条 SQL_SERVER 导oracle(转) SQL_SERVER 连接oracle(转) win7电脑上wifi Oracle对象统计信息 关于odp.net的FetchSize属性 技巧类 linq in 语法 温习设计模式 静态与非静态(转改) 关于引擎的设计
组合模式
zyi · 2013-07-05 · via 博客园 - zyi

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

        }

    }

}