Here, we will see how to create Mediator Design Pattern with example.
The Mediator design pattern centralizes communication between objects (colleagues) through a mediator. This prevents objects from communicating directly with one another, reducing dependencies and simplifying the system.
Example
An air traffic control system using the Mediator design pattern. The air traffic control tower centralizes communication between airplanes, coordinating take-offs and landings without the planes communicating directly with each other. We will explain in detail lator part of the article with code example.
A traffic control system is a real-world example of the mediator pattern. In an airport, for instance, an air traffic controller (mediator) coordinates the landings and take-offs of multiple airplanes (colleagues). Without the mediator, planes would have to communicate directly with each other to negotiate landing slots, leading to chaos. The air traffic controller centralizes the communication and directs planes as needed, ensuring that they don’t collide and that operations run smoothly.
The image should depict an airport traffic control system:
C# Code
using System;
using System.Collections.Generic;
// Mediator Interface
public interface ITrafficControlMediator
{
void RegisterAirplane(Airplane airplane);
void Notify(string message, Airplane airplane);
}
// Concrete Mediator
public class AirTrafficControl : ITrafficControlMediator
{
private List<Airplane> _airplanes = new List<Airplane>();
public void RegisterAirplane(Airplane airplane)
{
_airplanes.Add(airplane);
}
public void Notify(string message, Airplane airplane)
{
foreach (var otherAirplane in _airplanes)
{
if (otherAirplane != airplane)
{
otherAirplane.ReceiveMessage(message);
}
}
}
}
// Colleague Class
public class Airplane
{
private readonly ITrafficControlMediator _mediator;
public string Name { get; }
public Airplane(string name, ITrafficControlMediator mediator)
{
Name = name;
_mediator = mediator;
_mediator.RegisterAirplane(this);
}
public void SendRequest(string message)
{
Console.WriteLine($"{Name} sending message: {message}");
_mediator.Notify($"{Name}: {message}", this);
}
public void ReceiveMessage(string message)
{
Console.WriteLine($"{Name} received message: {message}");
}
}
// Example Usage
public class Program
{
public static void Main(string[] args)
{
ITrafficControlMediator atc = new AirTrafficControl();
Airplane airplane1 = new Airplane("Flight 101", atc);
Airplane airplane2 = new Airplane("Flight 202", atc);
Airplane airplane3 = new Airplane("Flight 303", atc);
airplane1.SendRequest("Requesting landing permission");
airplane2.SendRequest("Requesting takeoff clearance");
// Output:
// Flight 101 sending message: Requesting landing permission
// Flight 202 received message: Flight 101: Requesting landing permission
// Flight 303 received message: Flight 101: Requesting landing permission
// Flight 202 sending message: Requesting takeoff clearance
// Flight 101 received message: Flight 202: Requesting takeoff clearance
// Flight 303 received message: Flight 202: Requesting takeoff clearance
}
}
using System;
public interface IChatRoomMediator
{
void ShowMessage(User user, string message);
}
// Concrete Mediator
public class ChatRoom : IChatRoomMediator
{
public void ShowMessage(User user, string message)
{
Console.WriteLine($"{DateTime.Now} [{user.Name}] : {message}");
}
}
// Colleague Class
public class User
{
private string _name;
private IChatRoomMediator _chatMediator;
public User(string name, IChatRoomMediator chatMediator)
{
_name = name;
_chatMediator = chatMediator;
}
public string Name => _name;
public void SendMessage(string message)
{
_chatMediator.ShowMessage(this, message);
}
}
class Program
{
static void Main(string[] args)
{
var mediator = new ChatRoom();
var user1 = new User("Rohatash", mediator);
var user2 = new User("Mohit", mediator);
user1.SendMessage("Hello, Bob!");
user2.SendMessage("Hi Alice, how are you?");
}
}
Here, the ChatRoom acts as a mediator between User objects. Each user communicates through the chat room, reducing the need for direct coupling between users.
Output