Behavioral Design Pattern: Mediator
What is the Mediator pattern and how does it help?
Introduction
Behavior design patterns are a subset of design patterns that focus on how objects communicate and collaborate with each other. The Mediator pattern is one such behavior design pattern that allows loose coupling between objects by centralizing communication between them through a mediator object. This pattern helps in reducing direct dependencies and simplifies the interactions among objects, making the code more maintainable and scalable.
Understanding the Mediator Pattern
In software development, especially in large applications, classes often end up having many direct dependencies on each other. This can lead to a tangled web of communication, making it difficult to understand, maintain, and extend the codebase. The Mediator pattern addresses this issue by introducing a mediator object that acts as an intermediary between classes, handling all communications and interactions among them.
The key participants in the Mediator pattern are:
- Mediator: It defines the interface for communication between Colleague objects.
- Concrete Mediator: This class implements the Mediator interface and coordinates communication between Colleague objects.
- Colleague: These are individual classes that need to communicate with each other but do not do so directly. Instead, they communicate through the Mediator.
Before we jump onto a real-time example, let’s quickly take a look at the pros and cons of implementing a Mediator behavioral design pattern for your application.
Real-Time Example: Chat Room
Let’s understand the Mediator pattern with a real-time example of a chat room. Imagine we are building a chat application where multiple users can communicate with each other through messages. In this example, we will have the following components:
- ChatRoom (Mediator): Manages the communication between different users.
- User (Colleague): Represents an individual user in the chat room.
Step-by-Step Implementation
Step 1: Define the Colleague Interface
public interface IUser
{
string Name { get; }
void SendMessage(string message);
void ReceiveMessage(string sender, string message);
}
Step 2: Implement the Concrete Colleague
public class User : IUser
{
public string Name { get; private set; }
private readonly ChatRoom chatRoom;
public User(string name, ChatRoom chatRoom)
{
Name = name;
this.chatRoom = chatRoom;
}
public void SendMessage(string message)
{
chatRoom.SendMessage(this, message);
}
public void ReceiveMessage(string sender, string message)
{
Console.WriteLine($"{Name} received a message from {sender}: {message}");
}
}
Step 3: Implement the Mediator (Concrete Mediator)
public class ChatRoom
{
private readonly List<IUser> users = new List<IUser>();
public void RegisterUser(IUser user)
{
users.Add(user);
}
public void SendMessage(IUser sender, string message)
{
foreach (var user in users)
{
if (user != sender)
{
user.ReceiveMessage(sender.Name, message);
}
}
}
}
Step 4: Putting It All Together
Now, let’s use the Mediator pattern to enable communication between users through the chat room.
class Program
{
static void Main()
{
var chatRoom = new ChatRoom();
var user1 = new User("John", chatRoom);
var user2 = new User("Alice", chatRoom);
var user3 = new User("Bob", chatRoom);
chatRoom.RegisterUser(user1);
chatRoom.RegisterUser(user2);
chatRoom.RegisterUser(user3);
user1.SendMessage("Hello, everyone!");
user2.SendMessage("Hey, John!");
}
}
Output
The output of the above program will be:Alice received a message from John: Hello, everyone!
Bob received a message from John: Hello, everyone!
John received a message from Alice: Hey, John!
John received a message from Bob: Hey, John!
Conclusion
The Mediator pattern is an effective way to manage complex communication scenarios among objects. By introducing a mediator object, it centralizes the communication logic, reducing the direct dependencies between objects. This promotes loose coupling, making the codebase more maintainable and extensible.
In the real-time example of a chat room, we saw how the Mediator pattern facilitates communication between users by using a chat room as the mediator. The users can send messages to each other without knowing the details of how the messages are delivered. The Mediator pattern provides a flexible and scalable solution for handling communication in various scenarios, promoting a more modular and organized code structure.
If you enjoy the content I create and would like to show your appreciation, you can buy me a coffee!