中介者模式(Mediator Pattern),定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。 Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently。
中介者减少了各个同事对象的耦合,使得可以独立地改变和复用各个同事对象和中介者类;由于把对想如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到他们之间的交互上来,也就是站在一个更宏伟的角度去考虑系统。
中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。
Mediator类:抽象中介者,定义了同事对象交互的接口。
ConcreteMediator类:具体中介者对象,实现抽象类中的方法,此具体中介者对象需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发送命令。
Co
ConcreteCo
1、中介者类Mediator及其具体实现类ConcreteMediator
|
/// <summary>
/// The 'Mediator' abstract class
/// </summary>
abstract class Mediator
{
public abstract void Send(string message,Co
}
/// <summary>
/// The 'ConcreteMediator' class
/// </summary>
class ConcreteMediator : Mediator
{
private ConcreteCo
private ConcreteCo
public ConcreteCo
{
set { _co
}
public ConcreteCo
{
set { _co
}
public override void Send(string message,Co
{
if (co
{
_co
}
else
{
_co
}
}
}
|
2、抽象同事类Co
|
/// <summary>
/// The 'Co
/// </summary>
abstract class Co
{
protected Mediator mediator;
// Constructor
public Co
{
this.mediator = mediator;
}
}
/// <summary>
/// A 'ConcreteCo
/// </summary>
class ConcreteCo
{
// Constructor
public ConcreteCo
: base(mediator)
{
}
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("Co
}
}
/// <summary>
/// A 'ConcreteCo
/// </summary>
class ConcreteCo
{
// Constructor
public ConcreteCo
: base(mediator)
{
}
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("Co
}
}
|
3、客户端代码
|
static void
{
ConcreteMediator m = new ConcreteMediator();
ConcreteCo
ConcreteCo
m.Co
m.Co
c1.Send("How are you? JamesHao");
c2.Send("Fine, t
// Wait for user
Console.ReadKey();
}
|
实现一个聊天室功能,聊城室就是一个中介者,参与聊天的人就是同事对象,如下图所示
AbstractChatroom类:抽象聊天室类,做为Participant的交互的中介。
Register()方法:会员注册功能;Send()方法:发送消息功能。
Chatroom类:具体聊天室类,实现抽象聊天室类中的方法。
Participant类:参与者类,主要有发送消息Send()功能和接受消息Receive()功能。
Beatle类,NonBeatle类:参与者类的具体实现,实现父类Paticipant类中的方法。
1、抽象聊天室类AbstractChatroom及其具体聊天室Chatroom
|
/// <summary>
/// The 'Mediator' abstract class
/// </summary>
abstract class AbstractChatroom
{
public abstract void Register(Participant participant);
public abstract void Send(string from, string to, string message);
}
/// <summary>
/// The 'ConcreteMediator' class
/// </summary>
class Chatroom : AbstractChatroom
{
private Dictionary<string, Participant> _participants =new Dictionary<string, Participant>();
public override void Register(Participant participant)
{
if (!_participants.ContainsValue(participant))
{
_participants[participant.Name] = participant;
}
participant.Chatroom = this;
}
public override void Send(string from, string to, string message)
{
Participant participant = _participants[to];
if (participant != nu
{
participant.Receive(from, message);
}
}
}
|
2、参与者Participant类及其实现Beatle、NonBeatle
|
/// <summary>
/// The 'AbstractCo
/// </summary>
class Participant
{
private Chatroom _chatroom;
private string _name;
// Constructor
public Participant(string name)
{
this._name = name;
}
// Gets participant name
public string Name
{
get { return _name; }
}
// Gets chatroom
public Chatroom Chatroom
{
set { _chatroom = value; }
get { return _chatroom; }
}
// Sends message to given participant
public void Send(string to, string message)
{
_chatroom.Send(_name, to, message);
}
// Receives message from given participant
public virtual void Receive(string from, string message)
{
Console.WriteLine("{0} to {1}: '{2}'",from, Name, message);
}
}
/// <summary>
/// A 'ConcreteCo
/// </summary>
class Beatle : Participant
{
// Constructor
public Beatle(string name)
: base(name)
{
}
public override void Receive(string from, string message)
{
Console.Write("To a Beatle: ");
base.Receive(from, message);
}
}
/// <summary>
/// A 'ConcreteCo
/// </summary>
class NonBeatle : Participant
{
// Constructor
public NonBeatle(string name)
: base(name)
{
}
public override void Receive(string from, string message)
{
Console.Write("To a non-Beatle: ");
base.Receive(from, message);
}
}
|
3、客户端代码
|
static void
{
// Create chatroom
Chatroom chatroom = new Chatroom();
// Create participants and register them
Participant George = new Beatle("George");
Participant Paul = new Beatle("Paul");
Participant Ringo = new Beatle("Ringo");
Participant John = new Beatle("John");
Participant Yoko = new NonBeatle("Yoko");
chatroom.Register(George);
chatroom.Register(Paul);
chatroom.Register(Ringo);
chatroom.Register(John);
chatroom.Register(Yoko);
// Chatting participants
Yoko.Send("John", "Hi John!");
Paul.Send("Ringo", "A
Ringo.Send("George", "My sweet Lord");
Paul.Send("John", "Can't buy me love");
John.Send("Yoko", "My sweet love");
// Wait for user
Console.ReadKey();
}
|
中介者模式(Mediator Pattern),定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。
参考:http://www.dofactory.com/Patterns/PatternMediator.aspx