[源码下载]
乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
作者:webabcd
介绍
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
示例
有一个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;
/// <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; }
}
}
}
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;
/// <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; }
}
}
}
AbstractMessageMediator
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// 抽象中介者(Mediator)
/// </summary>
public abstract class AbstractMessageMediator
{
/// <summary>
/// 注册一个操作Message的对象
/// </summary>
/// <param name="AbstractMessage">AbstractMessage</param>
public abstract void Register(AbstractMessage AbstractMessage);
/// <summary>
/// 发送Message
/// </summary>
/// <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);
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// 抽象中介者(Mediator)
/// </summary>
public abstract class AbstractMessageMediator
{
/// <summary>
/// 注册一个操作Message的对象
/// </summary>
/// <param name="AbstractMessage">AbstractMessage</param>
public abstract void Register(AbstractMessage AbstractMessage);
/// <summary>
/// 发送Message
/// </summary>
/// <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
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// 中介者(ConcreteMediator)
/// </summary>
public class MessageMediator : AbstractMessageMediator
{
private Dictionary<string, AbstractMessage> _dictionary = new Dictionary<string, AbstractMessage>();
/// <summary>
/// 注册一个操作Message的对象
/// </summary>
/// <param name="abstractMessage">AbstractMessage</param>
public override void Register(AbstractMessage abstractMessage)
{
if (!_dictionary.ContainsKey(abstractMessage.UserId))
{
_dictionary.Add(abstractMessage.UserId, abstractMessage);
}
abstractMessage.AbstractMessageMediator = this;
}
/// <summary>
/// 发送Message
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="to">发送到UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
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;
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// 中介者(ConcreteMediator)
/// </summary>
public class MessageMediator : AbstractMessageMediator
{
private Dictionary<string, AbstractMessage> _dictionary = new Dictionary<string, AbstractMessage>();
/// <summary>
/// 注册一个操作Message的对象
/// </summary>
/// <param name="abstractMessage">AbstractMessage</param>
public override void Register(AbstractMessage abstractMessage)
{
if (!_dictionary.ContainsKey(abstractMessage.UserId))
{
_dictionary.Add(abstractMessage.UserId, abstractMessage);
}
abstractMessage.AbstractMessageMediator = this;
}
/// <summary>
/// 发送Message
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="to">发送到UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// 操作Message抽象类(Colleague)
/// </summary>
public abstract class AbstractMessage
{
private AbstractMessageMediator _abstractMessageMediator;
private string _userId;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public AbstractMessage(string userId)
{
this._userId = userId;
}
/// <summary>
/// UserId
/// </summary>
public string UserId
{
get { return _userId; }
}
/// <summary>
/// 中介者
/// </summary>
public AbstractMessageMediator AbstractMessageMediator
{
get { return _abstractMessageMediator; }
set { _abstractMessageMediator = value; }
}
/// <summary>
/// 发送Message(由客户端调用)
/// </summary>
/// <param name="to">发送到UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public string Send(string to, MessageModel mm)
{
return _abstractMessageMediator.Send(_userId, to, mm);
}
/// <summary>
/// 接受Message(由中介者调用)
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public abstract string Insert(string from, MessageModel mm);
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// 操作Message抽象类(Colleague)
/// </summary>
public abstract class AbstractMessage
{
private AbstractMessageMediator _abstractMessageMediator;
private string _userId;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public AbstractMessage(string userId)
{
this._userId = userId;
}
/// <summary>
/// UserId
/// </summary>
public string UserId
{
get { return _userId; }
}
/// <summary>
/// 中介者
/// </summary>
public AbstractMessageMediator AbstractMessageMediator
{
get { return _abstractMessageMediator; }
set { _abstractMessageMediator = value; }
}
/// <summary>
/// 发送Message(由客户端调用)
/// </summary>
/// <param name="to">发送到UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public string Send(string to, MessageModel mm)
{
return _abstractMessageMediator.Send(_userId, to, mm);
}
/// <summary>
/// 接受Message(由中介者调用)
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public abstract string Insert(string from, MessageModel mm);
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// Sql方式操作Message(ConcreteColleague)
/// </summary>
public class SqlMessage : AbstractMessage
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public SqlMessage(string userId)
: base(userId)
{
}
/// <summary>
/// 接受Message(由中介者调用)
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override string Insert(string from, MessageModel mm)
{
return "Sql方式插入Message(" + from + "发送给" + base.UserId + ")"
+ " - 内容:" + mm.Message
+ " - 时间:" + mm.PublishTime.ToString();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// Sql方式操作Message(ConcreteColleague)
/// </summary>
public class SqlMessage : AbstractMessage
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public SqlMessage(string userId)
: base(userId)
{
}
/// <summary>
/// 接受Message(由中介者调用)
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override string Insert(string from, MessageModel mm)
{
return "Sql方式插入Message(" + from + "发送给" + base.UserId + ")"
+ " - 内容:" + mm.Message
+ " - 时间:" + mm.PublishTime.ToString();
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// Xml方式操作Message(ConcreteColleague)
/// </summary>
public class XmlMessage : AbstractMessage
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public XmlMessage(string userId)
: base(userId)
{
}
/// <summary>
/// 接受Message(由中介者调用)
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override string Insert(string from, MessageModel mm)
{
return "Xml方式插入Message(" + from + "发送给" + base.UserId + ")"
+ " - 内容:" + mm.Message
+ " - 时间:" + mm.PublishTime.ToString();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/// <summary>
/// Xml方式操作Message(ConcreteColleague)
/// </summary>
public class XmlMessage : AbstractMessage
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
public XmlMessage(string userId)
: base(userId)
{
}
/// <summary>
/// 接受Message(由中介者调用)
/// </summary>
/// <param name="from">来自UserId</param>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public override string Insert(string from, MessageModel mm)
{
return "Xml方式插入Message(" + from + "发送给" + base.UserId + ")"
+ " - 内容:" + mm.Message
+ " - 时间:" + mm.PublishTime.ToString();
}
}
}
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.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("<br />");
Response.Write(user1.Send("user2", new MessageModel("不好就不好吧。", DateTime.Now)));
Response.Write("<br />");
Response.Write(user3.Send("user4", new MessageModel("吃了吗?", DateTime.Now)));
Response.Write("<br />");
Response.Write(user4.Send("user3", new MessageModel("没吃,你请我?", DateTime.Now)));
Response.Write("<br />");
Response.Write(user3.Send("user4", new MessageModel("不请。", DateTime.Now)));
Response.Write("<br />");
}
}
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("<br />");
Response.Write(user1.Send("user2", new MessageModel("不好就不好吧。", DateTime.Now)));
Response.Write("<br />");
Response.Write(user3.Send("user4", new MessageModel("吃了吗?", DateTime.Now)));
Response.Write("<br />");
Response.Write(user4.Send("user3", new MessageModel("没吃,你请我?", DateTime.Now)));
Response.Write("<br />");
Response.Write(user3.Send("user4", new MessageModel("不请。", DateTime.Now)));
Response.Write("<br />");
}
}
运行结果
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
参考
http://www.dofactory.com/Patterns/PatternMediator.aspx
OK
[源码下载]