I am here to continue the discussion around Design Patterns. Today we will go through one of the behavioral design patterns called Mediator.
Links to previous posts:
- Design Patterns Simplified: Part 1
- Design Patterns Simplified - Part 2 (Singleton)
- Design Patterns Simplified - Part 3 (Simple Factory)
- Design Patterns Simplified - Part 4 (Abstract Factory)
- Design Patterns Simplified - Part 5 (Factory Method)
- Design Patterns Simplified - Part 6 (Prototype)
- Design Patterns Simplified - Part 7 (Builder)
- Design Patterns Simplified - Part 8 (Facade)
- Design Patterns Simplified - Part 9 (Adapter)
- Design Patterns Simplified - Part 10 (Decorator)
- Design Patterns Simplified - Part 11 (Bridge)
- Design Patterns Simplified - Part 12 (Composite)
- Design Patterns Simplified - Part 13 (Proxy)
- Design Patterns Simplified - Part 14 (Command)
Before talking about its implementation let’s begin with defining it.
As per the GOF guys, Mediator pattern is defined as follows:
“Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.”
Well! Let’s understand what they mean and where this pattern can fit.
As the name suggests, Mediator pattern is used to enable interactions between source and target objects. It introduces a mediator object between source and target to avoid having explicit or direct references.
In a scenario when a lot of combinations take place between source and target and the number of interacting objects are growing over time, it becomes difficult to maintain code, especially for any new change.
Mediator pattern helps in such cases by encapsulating the communication logic in mediator object and thus supporting clean and loosely coupled design.

Mediator pattern has some key components as follows:
- Mediator
Defines an interface to facilitate communication among Colleague objects.
- ConcreteMediator
Implements Mediator and coordinates with colleague classes.
- ColleagueInterface
Interface or abstract class that defines common methods and properties to interact with colleagues.
- ColleagueClasses
Concrete colleague classes that are used by mediator.
How Mediator pattern works
Let’s understand this by a simple example.
We will start by defining Colleague abstract class. We can also use interface but I have taken abstract class to reuse properties.
- ///<summary>
- /// Abstract Colleague class
- ///</summary>
- abstract class Team
- {
- private string _name;
- public Team(string name)
- {
- _name = name;
- }
- public string Name
- {
- get
- {
- return _name;
- }
- }
- public MeetingRoom MeetingRoom
- {
- get;
- set;
- }
- public void SendMessage(string to, string message)
- {
- MeetingRoom.SendMessage(_name, to, message);
- }
- public virtual void ReceiveMessage(string from, string message)
- {
- Console.WriteLine("Message from {0} to {1}: {2}", from, _name, message);
- }
- }


Prakash TripathiPosted Jun 25, 2016, 3:06 AM
Thnx Santhakumar.
Santhakumar MunuswamyPosted Jun 25, 2016, 2:32 AM
Nice share
Prakash TripathiPosted Jun 17, 2016, 7:05 AM
Thnx Grzegorz.
Prakash TripathiPosted Jun 17, 2016, 7:04 AM
Thnx Vignesh.
Prakash TripathiPosted Jun 17, 2016, 7:04 AM
@Maruthi..I don't see any issue if user implements both the methods as both methods have significance.
Grzegorz OgrodPosted Jun 17, 2016, 5:58 AM
Really good article! Why I found today, not earlier? :)
Vignesh ManiPosted Jun 17, 2016, 5:18 AM
Good one
Maruthi PalllamalliPosted Jun 17, 2016, 1:54 AM
You shouldn't use interface for it. Because it is user choice - if we use interface surely implement both register() and sendmessage() functionalities. Hopefully it's denied
Prakash TripathiPosted Jun 16, 2016, 1:46 PM
@Maruthi, you may use abstract class as well. However since mediator class would be just one, having interface as base looks ok.
Maruthi PalllamalliPosted Jun 16, 2016, 4:58 AM
I am having one question in this your design pattern that is about mediator(interface IMeetingRoom). Here your mediator gives restriction to implementation. Why should i need to implement both Register( ) and sendmessage( ). You shouldn't give any restriction and should use abstract class instead of interface for mediator. Interaction should be in-dependable.
Prakash TripathiPosted Jun 16, 2016, 3:14 AM
Thnx Raja.
RajaPosted Jun 16, 2016, 3:05 AM
Good One...