Formycareer

积极生活每一天!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
class Program
{
static void Main(string[] args)
{
ConcreteMediator m
= new ConcreteMediator();
ConcreteColleagueA cA
= new ConcreteColleagueA(m);
ConcreteColleagueB cB
= new ConcreteColleagueB(m);
m.ColleagueA
= cA;
m.ColleagueB
= cB;
cA.Send(
"how are jim");
cB.Send(
"I'm fine,thank you!");
Console.Read();
}
}
abstract class Mediator
{
public abstract void Send(string msg, Colleague colleague);
}
class ConcreteMediator:Mediator
{
private ConcreteColleagueA _colleagueA;

internal ConcreteColleagueA ColleagueA
{
get { return _colleagueA; }
set { _colleagueA = value; }
}
private ConcreteColleagueB _colleagueB;

internal ConcreteColleagueB ColleagueB
{
get { return _colleagueB; }
set { _colleagueB = value; }
}
public override void Send(string msg, Colleague colleague)
{
if (colleague == _colleagueA)
_colleagueB.Notify(msg);
else if(colleague==_colleagueB)
_colleagueA.Notify(msg);
}
}
abstract class Colleague
{
protected Mediator mediator;
public Colleague(Mediator mediator)
{
this.mediator = mediator;
}
}
class ConcreteColleagueA : Colleague
{
public ConcreteColleagueA(Mediator mediator)
:
base(mediator)
{

}
public void Send(string msg)
{
mediator.Send(msg,
this);
}
public void Notify(string msg)
{
Console.WriteLine(
"ConcreteColleagueA's msg->"+msg);
}
}
class ConcreteColleagueB:Colleague
{
public ConcreteColleagueB(Mediator mediator)
:
base(mediator)
{

}
public void Send(string msg)
{
mediator.Send(msg,
this);
}
public void Notify(string msg)
{
Console.WriteLine(
"ConcreteColleagueB's msg->" + msg);
}
}

posted on 2011-04-08 14:22  mycareer  阅读(201)  评论(0编辑  收藏  举报