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);
}
}