Design Patterns,  C# Expert,  Information

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 the IMediator interface. It maintains a list of users and handles message distribution.
  • Define user classes (e.g., BasicUserPremiumUser) 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#:

UML class diagram of mediator design pattern c#
UML class diagram of mediator design pattern c#
In this diagram:
  • IUser is the interface for users in the chat application, with methods SendMessage() and ReceiveMessage().
  • User is the concrete implementation of IUser. It holds a reference to the mediator (IChatMediator) and has a Name.
  • IChatMediator is the interface for the mediator, defining the SendMessage() method.
  • ChatMediator is the concrete implementation of IChatMediator, 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *