Mediator Pattern using .NET. Introduction
Tags: Design Patterns, Mediator Pattern, C Sharp Programming

Introduction

Mediator pattern is a very well known pattern in programming and in this post, we will see what it is, what it brings to the table and a very simple implementation to understand some of the situations where we can think of using it.

A mediator pattern, encapsulate , how object’s interact and communicate with each other.

, how object’s with each other. It promotes loose coupling of objects.

It facilitates many-to-many relationship/communication as well as one-to-many.

relationship/communication as well as one-to-many. You can think of Mediator as a communication hub.

Examples

You can use mediator pattern for many different use-cases. Following are the few of the requirements/situations, where Mediator pattern can help us greatly.

Chat-Room(s)

Job/Task Process

Geo-Locations / Control Centers

You can implement this pattern in variety of ways in many different programming languages. I will be using C# in a .NET Core Console application using Visual Studio Code but the design can be easily ported to other languages.

Implementation using .NET Events (Control Center)

.NET Events are one of the way to implement Mediator Pattern and we will start with this example first. Later we will see another example which doesn’t uses Events for its implementation.

use-case: A control-center which receives notifications about vehicle movements.

I have simplified the requirements and here is what we want to achieve:

Whenever a vehicle is moved, it will broadcast its location to a central hub (mediator) and a control-center will be also connected to the hub and it will receive location updates as those are broadcasted from vehicles.

In real-world, vehicle might also want to communicate with each other or control-center personal want to communicate with all the drivers as well. But as long as you get the idea, you can extend this example to whole new levels. We will not implement these additional requirements today and will stick to our original basic use-case.

Vehicle Entity

Here is a very basic vehicle entity which is self explanatory.

So, our vehicle entity has a RegNo property and CurrentLocation. With these properties, we identify a vehicle and tell about its location. Then there is a Move method in which we are just setting some random values for its location.

The only thing remains is to broadcast this location update to central-hub and for that we need a Mediator. So now, lets move to build the mediator and then we will update Move method to wire things up.

Mediator (Central-Hub)

As mentioned above, you can think of it a central communication hub. So, objects will communicate to this hub and can receive notifications from this hub as well. This structure keeps things loosely coupled and easy to maintain the complex notifications.

Again, the code is very simple. Mediator class is implemented as a singleton which means that there will be only one instance of this class in our application.

A generic built-in EventHandler<T> LocationChanged will be registered by control-center to receive location changed notifications.

OnLocationChanged is the method which vehicles will call whenever they move.

Now, if you are wondering what is an event-handler and how they work, I wrote few posts about these topics and you can look those up to refresh some background on those topics.

Here is the our LocationChangedEventArgs class:

I could have passed the Vehicle as well from this class, because all this information is available in vehicle entity and that is also Ok.

Broadcast Location from Vehicles

We can now use Mediator from vehicle class (Move method) to broadcast location updates as shown below:

Receive Notifications in Control-Center

Here is our control-center:

In the constructor, we just register a handler-method to the Event raised by Mediator.

Program Execution

Here is main method which initiates this demonstration:

and the output shows our mediator in action:

Summary

Mediator pattern is a behavioral design pattern and it can help us greatly in messaging/notifications kind of scenarios while keeping objects loosely coupled. There are many different variations of this pattern and I will write about some of those variations in coming days. If you have any questions or comments, let me know. You can download the code from this github repo. Till next time, Happy Coding.

Originally published at https://hexquote.com on December 6, 2020.