樂在其中設計模式(C#) - 中介者模式(Mediator Pattern)
介紹
用一個中介對象來封裝一系列的對象互動。中介者使各對象不需要顯式地互相引用,進而使其耦合松散,而且可以獨立地改變它們之間的互動。
示例
有一個Message實體類,某個對象對它的操作有Send()和Insert()方法,現在用一個中介對象來封裝這一系列的對象互動。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/**//// <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;
/// Message内容
public string Message
get
{ return _message; }
set
{ _message = value; }
private DateTime _publishTime;
/// Message釋出時間
public DateTime PublishTime
{ return _publishTime; }
{ _publishTime = value; }
}
}
AbstractMessageMediator
/// 抽象中介者(Mediator)
public abstract class AbstractMessageMediator
/// 注冊一個操作Message的對象
/// <param name="AbstractMessage">AbstractMessage</param>
public abstract void Register(AbstractMessage AbstractMessage);
/// 發送Message
/// <param name="from">來自UserId</param>
/// <param name="to">發送到UserId</param>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public abstract string Send(string from, string to, MessageModel mm);
MessageMediator
/// 中介者(ConcreteMediator)
public class MessageMediator : AbstractMessageMediator
private Dictionary<string, AbstractMessage> _dictionary = new Dictionary<string, AbstractMessage>();
/// <param name="abstractMessage">AbstractMessage</param>
public override void Register(AbstractMessage abstractMessage)
if (!_dictionary.ContainsKey(abstractMessage.UserId))
_dictionary.Add(abstractMessage.UserId, abstractMessage);
}
abstractMessage.AbstractMessageMediator = this;
public override string Send(string from, string to, MessageModel mm)
AbstractMessage abstractMessage = _dictionary[to];
if (abstractMessage != null)
return abstractMessage.Insert(from, mm);
else
return null;
AbstractMessage
/// 操作Message抽象類(Colleague)
public abstract class AbstractMessage
private AbstractMessageMediator _abstractMessageMediator;
private string _userId;
/// <param name="userId">UserId</param>
public AbstractMessage(string userId)
this._userId = userId;
/// UserId
public string UserId
{ return _userId; }
/// 中介者
public AbstractMessageMediator AbstractMessageMediator
{ return _abstractMessageMediator; }
{ _abstractMessageMediator = value; }
/// 發送Message(由用戶端調用)
public string Send(string to, MessageModel mm)
return _abstractMessageMediator.Send(_userId, to, mm);
/// 接受Message(由中介者調用)
public abstract string Insert(string from, MessageModel mm);
SqlMessage
/// Sql方式操作Message(ConcreteColleague)
public class SqlMessage : AbstractMessage
public SqlMessage(string userId)
: base(userId)
public override string Insert(string from, MessageModel mm)
return "Sql方式插入Message(" + from + "發送給" + base.UserId + ")"
+ " - 内容:" + mm.Message
+ " - 時間:" + mm.PublishTime.ToString();
XmlMessage
/// Xml方式操作Message(ConcreteColleague)
public class XmlMessage : AbstractMessage
public XmlMessage(string userId)
return "Xml方式插入Message(" + from + "發送給" + base.UserId + ")"
Test
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.Mediator;
public partial class Mediator : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
AbstractMessageMediator messageMediator = new MessageMediator();
AbstractMessage user1 = new SqlMessage("user1");
AbstractMessage user2 = new SqlMessage("user2");
AbstractMessage user3 = new XmlMessage("user3");
AbstractMessage user4 = new XmlMessage("user4");
messageMediator.Register(user1);
messageMediator.Register(user2);
messageMediator.Register(user3);
messageMediator.Register(user4);
Response.Write(user1.Send("user2", new MessageModel("你好!", DateTime.Now)));
Response.Write("<br />");
Response.Write(user2.Send("user1", new MessageModel("我不好!", DateTime.Now)));
Response.Write(user1.Send("user2", new MessageModel("不好就不好吧。", DateTime.Now)));
Response.Write(user3.Send("user4", new MessageModel("吃了嗎?", DateTime.Now)));
Response.Write(user4.Send("user3", new MessageModel("沒吃,你請我?", DateTime.Now)));
Response.Write(user3.Send("user4", new MessageModel("不請。", DateTime.Now)));
運作結果
Sql方式插入Message(user1發送給user2) - 内容:你好! - 時間:2007-5-19 23:43:19
Sql方式插入Message(user2發送給user1) - 内容:我不好! - 時間:2007-5-19 23:43:19
Sql方式插入Message(user1發送給user2) - 内容:不好就不好吧。 - 時間:2007-5-19 23:43:19
Xml方式插入Message(user3發送給user4) - 内容:吃了嗎? - 時間:2007-5-19 23:43:19
Xml方式插入Message(user4發送給user3) - 内容:沒吃,你請我? - 時間:2007-5-19 23:43:19
Xml方式插入Message(user3發送給user4) - 内容:不請。 - 時間:2007-5-19 23:43:19
參考
<a href="http://www.dofactory.com/Patterns/PatternMediator.aspx" target="_blank">http://www.dofactory.com/Patterns/PatternMediator.aspx</a>
OK
<a href="http://files.cnblogs.com/webabcd/Pattern.rar">[源碼下載下傳]</a>