*Define an object that encapsulates how a set of objects interact.
** Mediator promotes loose coupling by keeping objects from referring to each other explicitly,
* it lets you vary their interaction independently.
class Program
{
static void Main(string[] args)
{
ConcreteMediator m = new ConcreteMediator();
ConcreteColleague1 c1 = new ConcreteColleague1(m);
ConcreteColleague2 c2 = new ConcreteColleague2(m);
m.Collegue1 = c1;
m.Collegue2 = c2;
c1.Send("How are you");
c2.Send("Fine, Thanks");
Console.ReadKey();
}
}
abstract class Mediator
{
public abstract void Send(string message, Colleague colleague);
}
abstract class Colleague
{
protected Mediator mediator;
public Colleague(Mediator mediator)
{
this.mediator = mediator;
}
}
class ConcreteColleague1 : Colleague
{
public ConcreteColleague1(Mediator mediator) : base(mediator) { }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("Colleague 1 get message" + message);
}
}
class ConcreteColleague2 : Colleague
{
public ConcreteColleague2(Mediator mediator) : base(mediator) { }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("Colleague 2 get message" + message);
}
}
class ConcreteMediator : Mediator
{
private ConcreteColleague1 _collegue1;
private ConcreteColleague2 _collegue2;
internal ConcreteColleague1 Collegue1
{
set { _collegue1 = value; }
}
internal ConcreteColleague2 Collegue2
{
set { _collegue2 = value; }
}
public override void Send(string message, Colleague colleague)
{
if (colleague == _collegue1)
_collegue2.Notify(message);
else
_collegue1.Notify(message);
}
}
** Mediator promotes loose coupling by keeping objects from referring to each other explicitly,
* it lets you vary their interaction independently.
class Program
{
static void Main(string[] args)
{
ConcreteMediator m = new ConcreteMediator();
ConcreteColleague1 c1 = new ConcreteColleague1(m);
ConcreteColleague2 c2 = new ConcreteColleague2(m);
m.Collegue1 = c1;
m.Collegue2 = c2;
c1.Send("How are you");
c2.Send("Fine, Thanks");
Console.ReadKey();
}
}
abstract class Mediator
{
public abstract void Send(string message, Colleague colleague);
}
abstract class Colleague
{
protected Mediator mediator;
public Colleague(Mediator mediator)
{
this.mediator = mediator;
}
}
class ConcreteColleague1 : Colleague
{
public ConcreteColleague1(Mediator mediator) : base(mediator) { }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("Colleague 1 get message" + message);
}
}
class ConcreteColleague2 : Colleague
{
public ConcreteColleague2(Mediator mediator) : base(mediator) { }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("Colleague 2 get message" + message);
}
}
class ConcreteMediator : Mediator
{
private ConcreteColleague1 _collegue1;
private ConcreteColleague2 _collegue2;
internal ConcreteColleague1 Collegue1
{
set { _collegue1 = value; }
}
internal ConcreteColleague2 Collegue2
{
set { _collegue2 = value; }
}
public override void Send(string message, Colleague colleague)
{
if (colleague == _collegue1)
_collegue2.Notify(message);
else
_collegue1.Notify(message);
}
}
No comments:
Post a Comment