原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern)

[索引页][源码下载]

乐在其中设计模式(C#) - 组合模式(Composite Pattern)

作者:webabcd





介绍

将对象组合成树形结构以表示“部分-整体”的层次结构。它使得客户对单个对象和复合对象的使用具有一致性。





示例

有一个Message实体类,使其单个对象和复合对象具有一致性。







MessageModel

using System;

using System.Collections.Generic;

using System.Text;



namespace Pattern.Composite

{

    /**//// <summary>

    /// Message实体类

    /// </summary>

    public class MessageModel

    {

        /**//// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="msg">Message内容</param>

        /// <param name="pt">Message发布时间</param>

        public MessageModel(string msg, DateTime pt)

        {

            this._message = msg;

            this._publishTime = pt;

        }



        private string _message;

        /**//// <summary>

        /// Message内容

        /// </summary>

        public string Message

        {

            get { return _message; }

            set { _message = value; }

        }



        private DateTime _publishTime;

        /**//// <summary>

        /// Message发布时间

        /// </summary>

        public DateTime PublishTime

        {

            get { return _publishTime; }

            set { _publishTime = value; }

        }

    }

}

MessageModelComponent

using System;

using System.Collections.Generic;

using System.Text;



namespace Pattern.Composite

{

    /**//// <summary>

    /// 抽象Message实体构件(Component)

    /// </summary>

    public abstract class MessageModelComponent

    {

        /**//// <summary>

        /// Message实体对象

        /// </summary>

        protected MessageModel _messageModel;



        /**//// <summary>

        /// 名称

        /// </summary>

        protected string _name;



        /**//// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="name">名称</param>

        /// <param name="mm">Message实体对象</param>

        public MessageModelComponent(string name, MessageModel mm)

        {

            this._name = name;

            this._messageModel = mm;

        }



        /**//// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="name">名称</param>

        public MessageModelComponent(string name)

        {

            this._name = name;

        }



        /**//// <summary>

        /// 添加

        /// </summary>

        /// <param name="mmc">MessageModelComponent</param>

        public abstract void Add(MessageModelComponent mmc);



        /**//// <summary>

        /// 删除

        /// </summary>

        /// <param name="mmc">MessageModelComponent</param>

        public abstract void Remove(MessageModelComponent mmc);



        /**//// <summary>

        /// 获取

        /// </summary>

        /// <param name="indent">缩进数</param>

        /// <returns></returns>

        public abstract string GetData(int indent);

    }

}

MessageModelLeaf

using System;

using System.Collections.Generic;

using System.Text;



namespace Pattern.Composite

{

    /**//// <summary>

    /// Message实体树叶(Leaf)

    /// </summary>

    public class MessageModelLeaf : MessageModelComponent

    {

        /**//// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="name">名称</param>

        /// <param name="mm">Message实体对象</param>

        public MessageModelLeaf(string name, MessageModel mm)

            : base(name, mm)

        {

            

        }



        /**//// <summary>

        /// 添加

        /// </summary>

        /// <param name="mmc">MessageModelComponent</param>

        public override void Add(MessageModelComponent mmc)

        {

            throw new Exception("不能添加");

        }



        /**//// <summary>

        /// 删除

        /// </summary>

        /// <param name="mmc">MessageModelComponent</param>

        public override void Remove(MessageModelComponent mmc)

        {

            throw new Exception("不能删除");

        }



        /**//// <summary>

        /// 获取

        /// </summary>

        /// <param name="indent">缩进数</param>

        /// <returns></returns>

        public override string GetData(int indent)

        {

            return new String('—', indent) + 

                "树叶名称:" + _name + 

                ";信息内容:" + _messageModel.Message + 

                "<br />";

        }

    }

}

MessageModelComposite

using System;

using System.Collections.Generic;

using System.Text;



namespace Pattern.Composite

{

    /**//// <summary>

    /// Message实体树枝(Composite)

    /// </summary>

    public class MessageModelComposite : MessageModelComponent

    {

        private List<MessageModelComponent> _list;



        /**//// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="name">名称</param>

        public MessageModelComposite(string name)

            : base(name)

        {

            _list = new List<MessageModelComponent>();

        }



        /**//// <summary>

        /// 添加

        /// </summary>

        /// <param name="mmc">MessageModelComponent</param>

        public override void Add(MessageModelComponent mmc)

        {

            _list.Add(mmc);

        }



        /**//// <summary>

        /// 删除

        /// </summary>

        /// <param name="mmc">MessageModelComponent</param>

        public override void Remove(MessageModelComponent mmc)

        {

            _list.Remove(mmc);

        }



        /**//// <summary>

        /// 获取

        /// </summary>

        /// <param name="indent">缩进数</param>

        /// <returns></returns>

        public override string GetData(int indent)

        {

            string s = new String('—', indent) + 

                "树枝名称:" + _name + 

                "<br />";



            foreach (MessageModelComponent mmc in _list)

            {

                s );

            }



            return s;

        }

    }

}

Test

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



using Pattern.Composite;



public partial class Composite : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        MessageModelComposite root = new MessageModelComposite("树枝A");



        root.Add(new MessageModelLeaf("树叶A", new MessageModel("Message内容A", DateTime.Now)));

        root.Add(new MessageModelLeaf("树叶B", new MessageModel("Message内容B", DateTime.Now)));



        MessageModelComposite comp = new MessageModelComposite("树枝B");



        comp.Add(new MessageModelLeaf("树叶C", new MessageModel("Message内容C", DateTime.Now)));

        comp.Add(new MessageModelLeaf("树叶D", new MessageModel("Message内容D", DateTime.Now)));



        root.Add(comp);



        root.Add(new MessageModelLeaf("树叶E", new MessageModel("Message内容E", DateTime.Now)));



        MessageModelLeaf l = new MessageModelLeaf("树叶F", new MessageModel("Message内容F", DateTime.Now));

        

        root.Add(l);

        root.Remove(l);



        Response.Write(root.GetData());

    }

}

运行结果

—树枝名称:树枝A

———树叶名称:树叶A;信息内容:Message内容A

———树叶名称:树叶B;信息内容:Message内容B

———树枝名称:树枝B

—————树叶名称:树叶C;信息内容:Message内容C

—————树叶名称:树叶D;信息内容:Message内容D

———树叶名称:树叶E;信息内容:Message内容E





参考

http://www.dofactory.com/Patterns/PatternComposite.aspx





OK

[源码下载]

乐在其中设计模式(C#) - 组合模式(Composite Pattern)的更多相关文章

  1. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  2. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

  3. python 设计模式之组合模式Composite Pattern

    #引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...

  4. 设计模式-12组合模式(Composite Pattern)

    1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...

  5. 设计模式系列之组合模式(Composite Pattern)——树形结构的处理

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  6. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  7. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

  8. java_设计模式_组合模式_Composite Pattern(2016-08-12)

    概念: 组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部分-整体模式,它使我们树 ...

  9. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

随机推荐

  1. OCP读书笔记(9) - 诊断数据库

    数据库恢复顾问 Data Recovery Advisor的命令行选项 1. 启动 RMAN 进程并连接到目标$ rman target=/ 2. 假设发生了某个错误,希望找出原因,使用 list f ...

  2. Android推断程序前后台状态

    public class AppStatusService extends Service { private static final String TAG = "AppStatusSer ...

  3. 从零開始学习制作H5应用——V5.0:懊悔机制,整理文件夹,压缩,模板化

    经过前面四个版本号的迭代.我们已经制作了一个从视觉和听觉上都非常舒服的H5微场景应用,没有看过的请戳以下: V1.0--简单页面滑动切换 V2.0--多页切换.透明过渡及交互指示 V3.0--增加lo ...

  4. 2014/08/23——OJ出现waiting...

    问题: 今天中午,裴主解决OJ他缓慢的问题后,开着.我跟着oj他递给发现了一个话题waiting该..... 和全哥.均觉得測评程序挂了.于是重新启动測系统,还waiting.....(測评系统的进程 ...

  5. 阿里云免费试用之体验——阿里云serverECS试用心得

    自上次參加了阿里云的开发人员大会回来 心里就一直惦记着阿里云 由于曾经各种各样什么的server也用了不少 年前開始接触阿里云 一直没有给予很多其它的关注 參加了这次的开发人员大会后 就想更进一步的了 ...

  6. 怎样使用 iOS 7 的 AVSpeechSynthesizer 制作有声书(1)

    原文: http://www.raywenderlich.com/64623/make-narrated-book-using-avspeechsynthesizer-ios-7 随着 PageVie ...

  7. WIP jobs available to MRP

    1. Supply 2. Demand 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  8. __iomem解析

    以下是在学习电池驱动中遇到的知识点之_iomem A new I/O memory access mechanism Most reasonably current cards for the PCI ...

  9. SWT的TableVierer的使用三(数据筛选和着色)

    如果我们想根据某一列来过滤记录,如何实现呢?很简单,定义一个过滤器filter.这里只演示定义一个过滤器的情况.现实中你可以定义多个灵活的过滤器,通过替换过滤器来实现各种各样的过滤.一.过滤器代码: ...

  10. 蓝缘管理系统第二个版本号开源了。springMVC+springSecurity3.x+Mybaits3.x 系统

    蓝缘管理系统第二个版本号开源了 继于 http://blog.csdn.net/mmm333zzz/article/details/16863543 版本号一.版本号二 对springMVC+spri ...