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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security 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();

        }

    }

}