组合模式(Composite Pattern)
介绍将对象组合成树形结构以表示“部分-整体”的层次结构。它使得客户对单个对象和复合对象的使用具有一致性。示例有一个Message实体类,使其单个对象和复合对象具有一致性。MessageModel
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Composite{ ////// Message实体类 /// public class MessageModel { ////// 构造函数 /// /// Message内容 /// Message发布时间 public MessageModel(string msg, DateTime pt) { this._message = msg; this._publishTime = pt; } private string _message; ////// Message内容 /// public string Message { get { return _message; } set { _message = value; } } private DateTime _publishTime; ////// Message发布时间 /// public DateTime PublishTime { get { return _publishTime; } set { _publishTime = value; } } }}
MessageModelComponent
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Composite{ ////// 抽象Message实体构件(Component) /// public abstract class MessageModelComponent { ////// Message实体对象 /// protected MessageModel _messageModel; ////// 名称 /// protected string _name; ////// 构造函数 /// /// 名称 /// Message实体对象 public MessageModelComponent(string name, MessageModel mm) { this._name = name; this._messageModel = mm; } ////// 构造函数 /// /// 名称 public MessageModelComponent(string name) { this._name = name; } ////// 添加 /// /// MessageModelComponent public abstract void Add(MessageModelComponent mmc); ////// 删除 /// /// MessageModelComponent public abstract void Remove(MessageModelComponent mmc); ////// 获取 /// /// 缩进数 ///public abstract string GetData(int indent); }}
MessageModelLeaf
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Composite{ ////// Message实体树叶(Leaf) /// public class MessageModelLeaf : MessageModelComponent { ////// 构造函数 /// /// 名称 /// Message实体对象 public MessageModelLeaf(string name, MessageModel mm) : base(name, mm) { } ////// 添加 /// /// MessageModelComponent public override void Add(MessageModelComponent mmc) { throw new Exception("不能添加"); } ////// 删除 /// /// MessageModelComponent public override void Remove(MessageModelComponent mmc) { throw new Exception("不能删除"); } ////// 获取 /// /// 缩进数 ///public override string GetData(int indent) { return new String('—', indent) + "树叶名称:" + _name + ";信息内容:" + _messageModel.Message + ""; } }}
MessageModelComposite
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Composite{ ////// Message实体树枝(Composite) /// public class MessageModelComposite : MessageModelComponent { private List_list; /// /// 构造函数 /// /// 名称 public MessageModelComposite(string name) : base(name) { _list = new List(); } /// /// 添加 /// /// MessageModelComponent public override void Add(MessageModelComponent mmc) { _list.Add(mmc); } ////// 删除 /// /// MessageModelComponent public override void Remove(MessageModelComponent mmc) { _list.Remove(mmc); } ////// 获取 /// /// 缩进数 ///public override string GetData(int indent) { string s = new String('—', indent) + "树枝名称:" + _name + " "; 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