二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern)
介绍
将对象组合成树形结构以表示“部分-整体”的层次结构。它使得客户对单个对象和复合对象的使用具有一致性。
示例
有一个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 += mmc.GetData(indent + 2);
} 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(1));
}
}
运行结果
—树枝名称:树枝A
———树叶名称:树叶A;信息内容:Message内容A
———树叶名称:树叶B;信息内容:Message内容B
———树枝名称:树枝B
—————树叶名称:树叶C;信息内容:Message内容C
—————树叶名称:树叶D;信息内容:Message内容D
———树叶名称:树叶E;信息内容:Message内容E
二十四种设计模式:组合模式(Composite Pattern)的更多相关文章
- 二十四种设计模式:适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...
- 二十四种设计模式:观察者模式(Observer Pattern)
观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- 二十四种设计模式:单例模式(Singleton Pattern)
单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...
随机推荐
- Non-constant Fields in Case Labels
Non-constant Fields in Case Labels in android library project http://tools.android.com/tips/non-cons ...
- android application plugins framework
android插件式开发 android application plugins framework http://code.google.com/p/android-application-plug ...
- C++数据结构之Stack(栈)
stack,栈,是好比堆积木似的数据结构,从上之下堆积,取出时按"LIFO"-last int first out后进先出的规则.栈一般为线程所独有,也就是每个线程有其自有的栈,与 ...
- C++中的::operator new, ::operator delete
一般在使用new 和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数 但是也有办法将这两个过程分开 那就是显式的调用::operat ...
- 在某个目录下的所有文件中查找包含某个字符串的Windows命令
findstr可以完成这个工作. 上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s 表示当前目录 ...
- 【Leetcode】 LRU Cache实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- css3 绘制优惠券
今天偶然发现了一个css3制作动画的地方,发现css3的径向渐变好难理解,幸亏有这里的大神介绍http://www.daqianduan.com/5989.html,这是优惠券的介绍 还有这个http ...
- IOS 作业项目(4)步步完成 画图 程序(中续)
一,程序布局整理 前言://1,程序启动//2,程序流程框架//3,程序界面一致//4,程序界面功能, //这里只做页面的固定功能, //在首次创建界面时,我们会指定好固定事件触发前的固定方法 //至 ...
- Tiling Up Blocks_DP
Description Michael The Kid receives an interesting game set from his grandparent as his birthday gi ...
- URl中文转ASCII
编码 System.Web.HttpUtility.UrlEncode("中国", System.Text.Encoding.GetEncoding("GB2312&qu ...