Mediator Design Pattern

Mediator Design Pattern

29 Mar 2024
Intermediate
63.6K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Mediator Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is mediator pattern and how is it work?

What is Mediator Design Pattern?

 Mediator Design Pattern allows multiple objects to communicate with each other without knowing each other’s structure. This pattern defines an object which encapsulates how the objects will interact with each other’s and support easy maintainability of the code by loose coupling.

This pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc.

Mediator Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Mediator Design Pattern is given below:

Mediator Design Pattern C#

The classes, interfaces, and objects in the above UML class diagram are as follows:

  1. Mediator

    This is an interface that defines operations which can be called by colleague objects for communication.

  2. ConcreteMediator

    This is a class that implements the communication operations of the Mediator interface.

  3. Colleague

    This is a class that defines a single, protected field that holds a reference to a mediator.

  4. ConcreteColleagueA/B

    These are the classes that communicate with each other via the mediator.

C# - Implementation Code

public abstract class Colleague
{
 protected IMediator _mediator;

 public Colleague(IMediator mediator)
 {
 _mediator = mediator;
 }
}

public class ConcreteColleagueA : Colleague
{
 public ConcreteColleagueA(IMediator mediator) : base(mediator) { }

 public void Send(string msg)
 {
 Console.WriteLine("A send message:" + msg);
 _mediator.SendMessage(this, msg);
 }

 public void Receive(string msg)
 {
 Console.WriteLine("A receive message:" + msg);
 }
}

public class ConcreteColleagueB : Colleague
{
 public ConcreteColleagueB(IMediator mediator) : base(mediator) { }

 public void Send(string msg)
 {
 Console.WriteLine("B send message:" + msg);
 _mediator.SendMessage(this, msg);
 }

 public void Receive(string msg)
 {
 Console.WriteLine("B receive message:" + msg);
 }
}

public interface IMediator
{
 void SendMessage(Colleague caller, string msg);
}

public class ConcreteMediator : IMediator
{
 public ConcreteColleagueA Colleague1 { get; set; }

 public ConcreteColleagueB Colleague2 { get; set; }

 public void SendMessage(Colleague caller, string msg)
 {
 if (caller == Colleague1)
 Colleague2.Receive(msg);
 else
 Colleague1.Receive(msg);
 }
}


Real Life Example:

Real Life Example of Mediator Design Pattern C#

When to use it?

  1. Communication between multiple objects is well defined but potentially complex.

  2. When too many relationships exist and a common point of control or communication is required.

  3. Some object can be grouped and customized based on behaviors.

Read More Articles Related to Design patterns
What do you think?

I hope you will enjoy the Mediator Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this