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

推荐订阅源

量子位
P
Privacy International News Feed
Security Latest
Security Latest
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Last Watchdog
The Last Watchdog
S
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Cyberwarzone
Cyberwarzone
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
A
Arctic Wolf
博客园 - 聂微东
I
Intezer
腾讯CDC
罗磊的独立博客
T
Tailwind CSS Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
N
News and Events Feed by Topic
SecWiki News
SecWiki News
S
Security Affairs
T
Threat Research - Cisco Blogs
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
T
Troy Hunt's Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
I
InfoQ
Hacker News: Ask HN
Hacker News: Ask HN
T
The Blog of Author Tim Ferriss
Schneier on Security
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - 阿修罗一平

IIS部署WCF出现的各种问题汇总 Infragistics控件的工具栏注册 NUnit的使用中可能遇到的问题 Linq备忘 Nihibernate的重要知识点 Devexpress使用记录 使用List数据集合,利用DevExpress.XtraReports开发Master-Detail报表 PDA访问WCF PDA(WinCE)项目开发中遇到的问题及解决方法总结 Grid控件绑定bindingSource后在新增行时设置Cell的初始值 “BindingSource绑定单个实体对象后在代码中赋值无效和无法显示”的解决方法 sqlservier2005转成sqlserver2000中出现的问题(WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) 项目考虑因素及解决方案(.net) 设计模式在工作中的应用(二) 设计模式在工作中的应用(一) NHibernate示例 Remoting服务集成到IIS的简单总结 关于Remoting服务启动和停止的简单总结 将公司系统从SqlServer 2K移植到Oracle 10g中的简要总结
设计模式在工作中的应用(三)
阿修罗一平 · 2007-10-15 · via 博客园 - 阿修罗一平
 

      设计并不是一定需要设计模式,有时使用设计模式还不如不使用设计模式更简洁。所以,我所讲述的模块设计中使用的设计模式不一定正确,只作为一种过程记录。

       框架的主题已经搭建完成,现在要让框架提供一些系统服务,好方便客户端的开发和使用服务端提供的服务。其中,有这样一个需求:客户端想要知道服务端服务目录下所有文件夹和文件的信息,并且客户端从服务端获取的数据可以方便以树型结构显示。

       拿到这个需求后,我是这么想的:

1、 服务端需要提供查询文件目录的服务,这个很好实现,调用.net提供的类库检索目录结构就可以了。功能实现后,还应该考虑一些什么呢?我想今天需要一个目录的信息服务,那明天会不会又要提供一个服务列表的服务呢?后天又会是什么?基于这种考虑,为了能不断扩展针对不同事物的信息获取,我选择了command模式,通过不同的命令对象实现不同的信息获取或者相关操作,方便以后不同信息服务的扩展。(这里并不是非得使用command模式,我只是考虑减少负责命令调用类和各种不同命令类之间的耦合。)

2、 信息获取后,应该以何种方式组织,这样客户端可方便遍历这些信息?针对文件目录特点和客户端以目录树结构显示要求,那么对于文件目录服务返回的目录信息组织是通过compostie模式实现。这样比将获得的字符信息连接起来传到客户端分析更有结构感。

设计简图:

InformationCmd:是信息命令的基类,可以继承它扩展其他的信息命令

FileCmd:文件命令,比如获取文件夹下的所有文件目录等

FileOperation::封装了文件及文件夹操作

InfoCompositeInfoLeafInfoContainer是用于保存获取的文件和目录信息

///<summary>

        ///显示树结构
       ///</summary>

        ///<param name="composite"></param>

        private void ShowTree(Sptes.Framework.Information.Common.InfoComposite<string> composite)

        {

            TreeNode root = new TreeNode();

            root.Text = composite.InfoData;

            this.treeView1.Nodes.Add(root);

            ShowTree(composite, root);           

        }

        ///<summary>

        ///显示树结构

        ///</summary>

        ///<param name="composite"></param>

        private void ShowTree(Sptes.Framework.Information.Common.InfoComposite<string> composite,TreeNode node)

        {

            foreach(InfoComposite<string> temp in composite.ItemList)

            {

                if(temp.IsContainer)

                {

                    TreeNode subNode = new TreeNode();

                    subNode.Text = temp.InfoData;

                    node.Nodes.Add(subNode);

                    ShowTree(temp, subNode);                   

                }

                else

                {

                    TreeNode subNode = new TreeNode();

                    subNode.Text = temp.InfoData;

                    node.Nodes.Add(subNode);

                }

            }

        }