Mediator Pattern C#
Contents
What Is Mediator Pattern?
The Mediator pattern in C# enables objects to communicate, without knowing each other’s identities. It also encapsulates a protocol that objects can follow.
You can think of a Mediator object as a kind of a coordinator; that handles traffic between appropriate parties based on its own logic.
Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
Mediator Pattern Example
Let’s start with the problem statement,
You are maintaining a network of computers in Mesh topology.
A mesh network is a network topology in which each node relays data for the network. All mesh nodes cooperate in the distribution of data in the network.
If a new computer is added or an existing computer is removed, all other computers in that network should know about these two events.
Let’s see how the Mediator pattern fits into it.
In the above example IMediator
defines the contract for communication between Colleagues. This communication is implemented in NetworkMediator
using delegates and events.
RegisterNotification
and UnRegisterNotification
are the two delegates that keep the track for the Colleague methods.
Here, Delegate form the basis for the event system in C#. For more information, see my blog post Delegates And Events In C#.
Although the example shows a very simple Mediator, it is possible that the mediator can have very complex logic. For example, messages can only be sent between colleagues of the same department.
Also,
ComputerColleague
class can maintain a private reference to the NetworkMediator
and thus register or unregister itself for notification. Let’s see the code example,
The Mediator pattern makes provisions for more than one mediator. For example, there may be many different departments in a company. Each department may have a different moderator, different rules of engagement, and a different list of users, but the structure of the lists is identical. Therefore, creating a new Mediator is merely an instantiation operation and does not require subclassing or an interface.
Where To Apply Mediator Pattern?
-
When a set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand.
-
When reusing an object is difficult because it refers to and communicates with many other objects.
-
When a behavior that’s distributed between several classes should be customizable without a lot of subclassing.
Note: You can download the complete solution demo from my github repository.
Further Reading
-
Observer Pattern C# - The observer design pattern enables a subscriber to register with and receive notifications from a provider. It is suitable for any scenario that requires push-based notification. The pattern defines a provider (also known as a subject or an observable) and zero, one, or more observers.
-
Publish Subscribe Design Pattern In C# - Publish Subscribe or Pub-Sub is a design pattern that allows loose coupling between the application components. This post explains the implementation detail of Pub-Sub using Delegates, EventHandlers and Event keyword in C#.
-
Using the event aggregator pattern to communicate between view models by Magnus Montin - In this post, Magnus explains how by introducing an event aggregator in between the publishers and subscribers, you can remove tight coupling between them. The subscriber observes the event aggregator instead of the publisher and the publisher knows only about the event aggregator and not about the subscribers. This pattern is much similar to the mediator pattern.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox