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.

  1 using System;
  2 
  3 class Program
  4 {
  5     static void Main(string[] args)
  6     {
  7         IMediator mediator = new NetworkMediator();
  8         ComputerColleague colleague1 = new ComputerColleague("Eagle");
  9         ComputerColleague colleague2 = new ComputerColleague("Ostrich");
 10         ComputerColleague colleague3 = new ComputerColleague("Penguin");
 11         mediator.Register(colleague1);
 12         mediator.Register(colleague2);
 13         mediator.Register(colleague3);
 14         mediator.Unregister(colleague1);
 15         Console.ReadLine();
 16 
 17         /*
 18         Output:
 19         New Computer register event with name:Ostrich: received @Eagle
 20         New Computer register event with name:Penguin: received @Eagle
 21         New Computer register event with name:Penguin: received @Ostrich
 22         Computer left unregister event with name:Eagle:received @Ostrich
 23         Computer left unregister event with name:Eagle:received @Penguin
 24         */
 25     }
 26 
 27     /* Define the contract for communication between Colleagues.
 28     Implementation is left to ConcreteMediator */
 29     interface IMediator
 30     {
 31         void Register(Colleague colleague);
 32         void Unregister(Colleague colleague);
 33     }
 34 
 35 
 36     /* Act as a central hub for communication between different Colleagues.
 37      Notifies all Concrete Colleagues on the occurrence of an event
 38     */
 39     class NetworkMediator : IMediator
 40     {
 41         public event EventHandler<ColleagueArgs> RegisterNotification = delegate { };
 42         public event EventHandler<ColleagueArgs> UnRegisterNotification = delegate { };
 43         public NetworkMediator()
 44         {
 45         }
 46         public void Register(Colleague colleague)
 47         {
 48             RegisterNotification(this, new ColleagueArgs(colleague));
 49             RegisterNotification += colleague.ReceiveRegisterNotification;
 50             UnRegisterNotification += colleague.ReceiveUnRegisterNotification;
 51         }
 52         public void Unregister(Colleague colleague)
 53         {
 54             RegisterNotification -= colleague.ReceiveRegisterNotification;
 55             UnRegisterNotification -= colleague.ReceiveUnRegisterNotification;
 56             UnRegisterNotification(this, new ColleagueArgs(colleague));
 57         }
 58     }
 59 
 60     public class ColleagueArgs : EventArgs
 61     {
 62         public ColleagueArgs(Colleague colleague)
 63         {
 64             Colleague = colleague;
 65         }
 66 
 67         public Colleague Colleague { get; }
 68     }
 69 
 70     /* Define the contract for notification events from Mediator.
 71      Implementation is left to ConcreteColleague
 72     */
 73     public abstract class Colleague
 74     {
 75         private String name;
 76         public Colleague(String name)
 77         {
 78             this.name = name;
 79         }
 80         public override String ToString()
 81         {
 82             return name;
 83         }
 84         public abstract void ReceiveRegisterNotification(
 85             object sender, ColleagueArgs colleagueArgs);
 86         public abstract void ReceiveUnRegisterNotification(
 87             object sender, ColleagueArgs colleagueArgs);
 88     }
 89 
 90     /* Process notification event raised by other Colleague through Mediator.
 91     */
 92     class ComputerColleague : Colleague
 93     {
 94         public ComputerColleague(string name) : base(name)
 95         {
 96         }
 97 
 98         public override void ReceiveRegisterNotification(
 99             object sender, ColleagueArgs colleagueArgs)
100         {
101             Console.WriteLine("New Computer register event with name:" 
102                 + colleagueArgs.Colleague + ": received @" + this);
103             // Send further messages to this new Colleague from now onwards
104         }
105 
106         public override void ReceiveUnRegisterNotification(
107             object sender, ColleagueArgs colleagueArgs)
108         {
109             Console.WriteLine("Computer left unregister event with name:" 
110                 + colleagueArgs.Colleague + ":received @" + this);
111             // Do not send further messages to this Colleague from now onwards
112         }
113     }
114 }

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,

 1 class ComputerColleague : Colleague
 2 {
 3     private readonly NetworkMediator mediator;
 4 
 5     public ComputerColleague(string name, NetworkMediator mediator) : base(name)
 6     {
 7         this.mediator = mediator;
 8         this.mediator.RegisterNotification += ReceiveRegisterNotification;
 9         this.mediator.UnRegisterNotification += ReceiveUnRegisterNotification;
10         
11     }
12 
13     public void UnRegister()
14     {
15         mediator.RegisterNotification -= ReceiveRegisterNotification;
16         mediator.UnRegisterNotification -= ReceiveUnRegisterNotification;
17     }
18 
19     ...
20     ...
21 }

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.

Mediator Pattern C#
Share this

Subscribe to Code with Shadman