The Mediator Pattern allows objects to communicate with each other through a common instance of a mediator class. It promotes loose coupling and prevents objects from referring to each other directly.
Through a common instance of a mediator the classes subscribing to it can communicate by sending messages to this instance and it will notify the other subscribed instances.
Figure 1: Subscribed instances
- public abstract class Mediator
- {
- public IList<AirCraft> AirCrafts { get; private set; }
-
- public Mediator()
- {
- AirCrafts = new List<AirCraft>();
- }
-
- public abstract void Send(AirCraft sender, string message);
- }
-
- public class CommunicationTower : Mediator
- {
- public override void Send(AirCraft sender, string message)
- {
- foreach (var airCraft in AirCrafts)
- {
- if (airCraft != sender)
- {
- airCraft.Receive(sender, message);
- }
- }
- }
- }
-
- public abstract class AirCraftCollegue
- {
- public abstract void Receive(AirCraft sender, string message);
- }
-
- public class AirCraft : AirCraftCollegue
- {
- public AirCraft(CommunicationTower mediator)
- {
- mediator.AirCrafts.Add(this);
- }
-
- public string Name { get; set; }
-
- public override void Receive(AirCraft sender, string message)
- {
- Console.WriteLine("{0}: Received message '{1}' from '{2}'", Name, message, sender.Name);
- }
- }
In the preceding example the mediator tells the subscribed aircrafts (except the sender) that one of them is saying something.
Example usage:
- class Program
- {
- static void Main(string[] args)
- {
- var towerMediator = new CommunicationTower();
-
- var airCraft1 = new AirCraft(towerMediator) { Name = "Unit #1" };
- var airCraft2 = new AirCraft(towerMediator) { Name = "Unit #2" };
- var airCraft3 = new AirCraft(towerMediator) { Name = "Unit #3" };
- var airCraft4 = new AirCraft(towerMediator) { Name = "Unit #4" };
- var airCraft5 = new AirCraft(towerMediator) { Name = "Unit #5" };
-
- towerMediator.Send(airCraft1, "Let's go up!");
- }
- }
Output:
- Unit #2: Received message 'Let's go up!' from 'Unit #1'
- Unit #3: Received message 'Let's go up!' from 'Unit #1'
- Unit #4: Received message 'Let's go up!' from 'Unit #1'
- Unit #5: Received message 'Let's go up!' from 'Unit #1'
One actual example is the EventAggreagator that we see in WPF. It allows the communication between view models that don't know about the existence of each other.