返回“設計模式(C#)系列文章索引”
介紹
將對象組合成樹形結構以表示“部分-整體”的層次結構。它使得客戶對單個對象和復合對象的使用具有一致性。
示例
有一個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
參考
http://www.dofactory.com/Patterns/PatternComposite.aspx
OK