Mediator pattern c# example 2024 (Mediator design pattern c#)
The Mediator Design Pattern in c# falls under the category of Behavioral Design Patterns. Mediator Pattern c#‘s primary purpose is to reduce communication complexity between multiple objects by centralizing complex communications and control. Here are the key points:
What is the Mediator Design Pattern?
According to the Gang of Four (GoF) definitions, the Mediator pattern defines an object that encapsulates how a set of objects interact with each other. It promotes loose coupling by preventing objects from explicitly referring to each other. Instead, they collaborate only via a mediator object. The mediator handles communication complexities between different objects.
Example of Mediator Design Pattern in C#: Facebook Group and ATC
Imagine a Facebook group where members can post messages, comment, and interact with each other. Instead of direct communication between members, a central mediator (the Facebook group itself) handles message routing. Members post messages to the group, and the group ensures that the messages reach the intended recipients. This reduces tight coupling between individual members.
Implementing the Mediator Design Pattern in C#
To implement the Mediator pattern in C#, follow these steps:
- Define an
IMediator
interface with methods for adding users and sending messages. - Create a concrete mediator class (
ChatMediator
) that implements theIMediator
interface. It maintains a list of users and handles message distribution. - Define user classes (e.g.,
BasicUser
,PremiumUser
) that interact with the mediator. Users send messages to the mediator, which then relays them to other users.
Advantages of Mediator Design Pattern in C#:
- Reduced Coupling: Objects communicate indirectly through the mediator, reducing direct dependencies.
- Centralized Control: The mediator acts as a communication center, making it easier to manage interactions.
- Scalability: As the number of objects grows, the mediator pattern simplifies communication management.
When to Use Mediator Design Pattern in C#?
Use the Mediator pattern when:
- You have a complex system with many interacting objects.
- The aim is to minimize tight coupling between objects.
- You need a centralized way to manage communication and control.
Know more about What is middleware in c#
UML Class Diagram Mediator Pattern c#
Here’s a simplified UML class diagram for the Mediator Design Pattern in c#:
In this diagram:
IUser
is the interface for users in the chat application, with methodsSendMessage()
andReceiveMessage()
.User
is the concrete implementation ofIUser
. It holds a reference to the mediator (IChatMediator
) and has aName
.IChatMediator
is the interface for the mediator, defining theSendMessage()
method.ChatMediator
is the concrete implementation ofIChatMediator
, managing the communication between users. It holds a list of users.
Arrows represent associations between classes, indicating that User
objects interact with ChatMediator
objects through the IChatMediator
interface. The User
class implements IUser
interface, while ChatMediator
class implements IChatMediator
interface. Check new features of .Net 9
Mediator Design Pattern in C# Code example
Let’s consider a chat application where multiple users can send messages to each other. Instead of each user directly communicating with every other user, which would result in tight coupling between users, we can implement a mediator to manage the communication between them.
using System;
using System.Collections.Generic;
// Mediator interface
interface IChatMediator
{
void SendMessage(string message, IUser user);
}
// Concrete mediator
class ChatMediator : IChatMediator
{
private List<IUser> users = new List<IUser>();
public void AddUser(IUser user)
{
users.Add(user);
}
public void SendMessage(string message, IUser user)
{
foreach (var u in users)
{
if (u != user) // Don't send message to the sender
u.ReceiveMessage(message);
}
}
}
// Colleague interface
interface IUser
{
void SendMessage(string message);
void ReceiveMessage(string message);
}
// Concrete colleague
class User : IUser
{
private IChatMediator mediator;
public string Name { get; private set; }
public User(IChatMediator mediator, string name)
{
this.mediator = mediator;
this.Name = name;
}
public void SendMessage(string message)
{
Console.WriteLine($"{Name} sends message: {message}");
mediator.SendMessage(message, this);
}
public void ReceiveMessage(string message)
{
Console.WriteLine($"{Name} receives message: {message}");
}
}
class Program
{
static void Main(string[] args)
{
IChatMediator chatMediator = new ChatMediator();
IUser user1 = new User(chatMediator, "User1");
IUser user2 = new User(chatMediator, "User2");
IUser user3 = new User(chatMediator, "User3");
chatMediator.AddUser(user1);
chatMediator.AddUser(user2);
chatMediator.AddUser(user3);
user1.SendMessage("Hello, everyone!");
user2.SendMessage("Hey, User1!");
}
}
In this example, the ChatMediator
acts as a mediator between users (User
objects). Users send messages through the mediator, which then distributes the message to all other users except the sender. This way, users are decoupled from each other, and new users can be added or removed from the chat without affecting the existing users or the mediator.
You can check about SOLID Principle in c#.
Remember that the actual implementation may involve additional details and methods, but this gives you an idea of the relationships between the classes.
- Asafoetida(Hing) – Bold and Earthy Character
- Best Samsung Galaxy Tab : S Pen Stylus Samsung Tab S6 Lite – S6 samsung
- Exploring the Power of Online Tools: EMI Calculator, Age Calculator, and Online GUID Generator
- How to Answer Salary Expectations: A Guide to Nailing the Salary Question
- How to Make Money Online: 13 Best Ways to Earn Money Online
- 13 Tips : How to Start Blogging for Free and Make Money
- How to Create Backlinks: A Step-by-Step Guide
- Difference between Scoped vs Transient vs Singleton in .NET Core
- Unlock the Power of Implementing Two Interface with Same Method in C#: Unraveling Multiple Inheritance
- Serialization and Deserialization in C#