Definition
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.
Summary
You should use the pattern in the following cases:
- Behavior that is distributed between some objects can be grouped or customized.
- Object reuse is difficult because it communicates with other objects.
- Objects in the system communicate in well-defined but complex ways.
UML class diagram
Participants
The classes and/or objects participating in this pattern are:
- Mediator - defines an interface for communicating with Colleague objects
- ConcreteMediator
knows and maintains its colleagues
- Colleague classes
each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague
Sample code in C#
- region Mediator
public interface Mediator
{
#region Methods
void Send(string message, Colleague colleague);
#endregion
}
#endregion
#region Concrete Mediator
public class ConcreteMediator : Mediator
{
#region Properties
public ListColleagues { get; private set; }
#endregion
#region Ctor
public ConcreteMediator()
{
Colleagues = new List();
}
#endregion
#region Mediator Members
public void Send(string message, Colleague colleague)
{
foreach (Colleague currentColleague in Colleagues)
{
if (!currentColleague.Equals(colleague))
{
currentColleague.Recieve(message);
}
}
}
#endregion
}
#endregion
#region Colleague
public abstract class Colleague
{
#region Members
protected Mediator _mediator;
#endregion
#region Ctor
public Colleague(Mediator mediator)
{
_mediator = mediator;
}
#endregion
#region Methods
///
/// Sends the given message
///
/// The given message
public abstract void Send(string message);
///
/// Recieves the given message
///
/// The given message
public abstract void Recieve(string message);
#endregion
}
#endregion
#region Concrete Colleague
public class ConcreteColleague : Colleague
{
#region Properties
public int ID { get; set; }
#endregion
#region Ctor
public ConcreteColleague(Mediator mediator, int id)
: base(mediator)
{
ID = id;
}
#endregion
#region Methods
public override void Send(string message)
{
_mediator.Send(message, this);
}
public override void Recieve(string message)
{
Console.WriteLine("{0} recieved the message: {1}",
ID, message);
}
#endregion
}
#endregion
As can be seen in the example, I have a mediator object that sends messages to the concretes of the colleague class. The example is simple but it shows the concepts that are used in the mediator pattern. With few modifications you
can use the example to build a small chat room application.
The following code was used to test the previous example:
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleague colleague1 = new ConcreteColleague(mediator, 1);
ConcreteColleague colleague2 = new ConcreteColleague(mediator, 2);
mediator.Colleagues.Add(colleague1);
mediator.Colleagues.Add(colleague2);
colleague1.Send("Hello from colleague 1");
colleague2.Send("Hello from colleague 2");
Console.Read();
-
-
Lectures(1115)
-
Trackback(0)
-
Permalien
Commentaires (0)