原文:乐在其中设计模式(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 += 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
[源码下载]乐在其中设计模式(C#) - 组合模式(Composite Pattern),布布扣,bubuko.com
乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:http://www.cnblogs.com/lonelyxmas/p/3590338.html