Mediator Design Pattern
Biplob Hosen
Senior Software Engineer | C# | .NET Core | MS SQL | PostgreSQL | Typescript | Microservice | Docker | Kubernetes | System Design
The Mediator design pattern is a behavioral design pattern that reduces the chaos of direct communication between multiple objects by introducing a mediator object. The mediator handles the communication and encapsulates how these objects interact. This pattern is particularly useful when you have a complex system with many interacting components.
Coupling Between the Objects
Let us understand the Mediator Design Pattern in C# with an Example. Please have a look at the following diagram. As shown in the diagram below, we have four objects (Object A, Object B, Object C, and Object D). And these four objects want to communicate with each other. Suppose Object A wants to communicate with Object B, then Object A should know the reference of Object B, and using that reference, Object A can call the method of Object B. Similarly if Object B wants to send some message to Object C. It should know the reference of Object C, and using that reference, it will call the methods of Object C and send the message.
The couplings between the objects are more, i.e., tight coupling. A lot of object knows each other. Now, in the above image, four objects are there. In real-time, you may have thousands of objects, and those thousands of objects want to communicate with each other. Then, writing code and maintaining code is very difficult.
How do we Reduce the Coupling Between the Objects?
Using the Mediator Design Pattern, we can reduce the coupling between objects. To understand this, please have a look at the following diagram. As shown in the image below, here we introduce the Mediator object. Each object has to send messages to the mediator object. The mediator object here will receive the message from each object and route that message to the destination object. So, the mediator object will take care of handling the messages. So, in this scenario, we can use the Mediator Design Pattern.
Real-Time Example to Understand Mediator Design Pattern – Facebook Group
Please have a look at the following diagram. Nowadays, everybody is aware of Facebook. On Facebook, we can create some specific groups; in those groups, people can join and share their knowledge. On Facebook, there is a group called Dot Net Tutorials, and in that group, many people (such as Ram, Sam, Pam, Dave, Steve, Anurag, etc.) are joined. Ram is sharing some messages in the Dot Net Tutorials group. Then, what this Facebook Group (Dot Net Tutorials) will do is it will send that message to all the members who have joined this group. So, here, the Facebook group is acting as a Mediator.
Example Facebook group is acting as a Mediator
Step1: Client
// Create instances of Mediator.
IFacebookGroupMediator facebookMediator = new ConcreteFacebookGroupMediator();
// Create instances of users.
User manun = new ConcreteUser("Manun");
User biplob = new ConcreteUser("Biplob");
User imrul = new ConcreteUser("Imrul");
User saddam = new ConcreteUser("Saddam");
User kabir = new ConcreteUser("Kabir");
User taijul = new ConcreteUser("Taijul");
// Register the users with the mediator.
facebookMediator.RegisterUser(biplob);
facebookMediator.RegisterUser(manun);
facebookMediator.RegisterUser(imrul);
facebookMediator.RegisterUser(saddam);
facebookMediator.RegisterUser(kabir);
facebookMediator.RegisterUser(taijul);
// Users sending messages in the Facebook Group.
biplob.Send("Hi everyone how are you?");
Console.WriteLine();
kabir.Send("Do you know What is Design Patterns? explain");
Step 2: Creating Mediator
// Mediator Interface
public interface IFacebookGroupMediator
{
void RegisterUser(User user);
void SendMessage(string msg, User user);
}
Step 3: Creating ConcreteMediator
public class ConcreteFacebookGroupMediator : IFacebookGroupMediator
{
private List<User> usersList = new List<User>();
public void RegisterUser(User user)
{
usersList.Add(user);
user.Mediator = this;
}
public void SendMessage(string message, User user)
{
foreach (User u in usersList)
{
if (u != user)
{
u.Receive(message);
}
}
}
}
领英推荐
Step 4: Creating the User
// User
public abstract class User
{
protected string Name;
public IFacebookGroupMediator Mediator { get; set; }
public User(string name)
{
this.Name = name;
}
public abstract void Send(string message);
public abstract void Receive(string message);
}
Step 5: Creating Concrete User
public class ConcreteUser : User
{
public ConcreteUser(string name) : base(name)
{
}
public override void Receive(string message)
{
Console.WriteLine(this.Name + ": Received Message: " + message);
}
public override void Send(string message)
{
Console.WriteLine(this.Name + ": Sending Message = " + message + "\n");
Mediator.SendMessage(message, this);
}
}
Output
When to Use Mediator Design Pattern?
·? Complex Communication Logic:
·? Reduce Coupling:
·? Centralize Control:
·? Simplify Object Protocols:
·? Event Handling Systems:
·? Reusing Components: