Mediator pattern in C#
Hello, C# lovers! š
In this post, I’m going to talk about the Mediator pattern and how you can use it in your C# projects. The Mediator pattern is a behavioral design pattern that helps you reduce the complexity of communication between multiple objects. It does this by introducing a mediator object that acts as a central hub for all the interactions between different objects. This way, the objects don’t need to know each other’s identities or methods, they just need to know the mediator. š
Why use the Mediator pattern?
The Mediator pattern can help you achieve several benefits, such as:
- Loose coupling: The objects are not directly dependent on each other, which makes them easier to reuse and test.
- Single responsibility: The mediator object encapsulates the logic of communication, which makes it easier to modify and maintain.
- Open/closed principle: You can add new objects or change their behavior without affecting the existing ones, as long as they conform to the mediator’s protocol.
How to implement the Mediator pattern in C#?
There are different ways to implement the Mediator pattern in C#, but one of the most common ones is using an abstract mediator class that defines an interface for communication, and a concrete mediator class that implements it. The mediator class usually has references to all the colleague objects that it mediates, and it can register or unregister them dynamically. The colleague objects are usually abstract classes or interfaces that define a common method for sending messages to the mediator. The concrete colleague classes implement this method and also have a method for receiving messages from the mediator.
Here is an example of how you can implement the Mediator pattern in C#:
// The abstract mediator class
public abstract class Mediator
{
// The method for sending messages to colleagues
public abstract void Send(string message, BtcExchange colleague);
}
// The concrete mediator class
public class ExchangeRateService : Mediator
{
// A dictionary of registered colleagues
private Dictionary<string, BtcExchange> subscribers = new Dictionary<string, BtcExchange>();
// A method for registering colleagues
public void Register(BtcExchange subscriber)
{
if (!subscribers.ContainsValue(subscriber))
{
subscribers[subscriber.Name] = subscriber;
}
subscriber.ExchangeRateService = this;
}
// A method for sending messages to colleagues
public override void Send(string message, BtcExchange subscriber)
{
if (subscribers.ContainsValue(subscriber))
{
// Send the message to all colleagues except the sender
foreach (var s in subscribers.Values)
{
if (s != subscriber)
{
s.Receive(message);
}
}
}
}
}
// The abstract colleague class
public abstract class BtcExchange
{
// A property for getting or setting the mediator
public Mediator ExchangeRateService { get; set; }
// A property for getting the colleague name
public string Name { get; }
// A constructor that takes a name as a parameter
public BtcExchange(string name)
{
Name = name;
}
// A method for sending messages to the mediator
public void Send(string message)
{
ExchangeRateService.Send(message, this);
}
// An abstract method for receiving messages from the mediator
public abstract void Receive(string message);
}
// A concrete colleague class
public class Subscriber : BtcExchange
{
// A constructor that takes a name as a parameter and calls the base constructor
public Subscriber(string name) : base(name) { }
// A method for receiving messages from the mediator
public override void Receive(string message)
{
Console.WriteLine($"{Name} received: {message}");
}
}
How to use the Mediator pattern in C#?
To use the Mediator pattern in C#, you need to create an instance of the concrete mediator class and register some instances of the concrete colleague classes with it. Then, you can use the Send method of any colleague object to send a message to all other colleagues through the mediator. Here is an example of how you can use the Mediator pattern in C#:
// Create an exchange rate service instance
var exchangeRateService = new ExchangeRateService();
// Create some subscriber instances
var binance = new Subscriber("Binance");
var coinbase = new Subscriber("Coinbase");
var kraken = new Subscriber("Kraken");
// Register them with the exchange rate service
exchangeRateService.Register(binance);
exchangeRateService.Register(coinbase);
exchangeRateService.Register(kraken);
// Send some messages
binance.Send("The current BTC/USD rate is 50,000");
coinbase.Send("The current BTC/EUR rate is 42,000");
kraken.Send("The current BTC/GBP rate is 36,000");
The output of this code is:
Coinbase received: The current BTC/USD rate is 50,000
Kraken received: The current BTC/USD rate is 50,000
Binance received: The current BTC/EUR rate is 42,000
Kraken received: The current BTC/EUR rate is 42,000
Binance received: The current BTC/GBP rate is 36,000
Coinbase received: The current BTC/GBP rate is 36,000
Where to learn more about the Mediator pattern in C#?
- Mediator Pattern C# - Code with Shadman
- Mediator Design Pattern in C# with Examples - Dot Net Tutorials
- C# Mediator Design Pattern - Dofactory
I hope you enjoyed this post and learned something new about the Mediator pattern in C#. If you have any questions or feedback, feel free to leave a comment below.
You can find the code examples here: source code
Happy coding! š
Comments
Post a Comment