Please enable Javascript to correctly display the contents on Dot Net Tricks!

Mediator Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 23 Feb 2017
Total Views : 1,417   
Updated On : 23 Feb 2017
 

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 share what is mediator pattern and how is it work?

What is Mediator Design Pattern?

This pattern allows multiple objects to communicate with each other’s without knowing each other’s structure. This pattern defines an object which encapsulates how the objects will interact with each other’s and supports 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 implement 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.

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
23 MAY
Xamarin Forms : Build Cross-platform Apps (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
5 MAY
ASP.NET MVC with Angular4 Development (Online)

06:30 AM-08:30 AM IST(+5.30 GMT)

Know More
29 APR
Xamarin Forms : Build Cross-platform Apps (Classroom)

5:00 PM-6:30 PM

Know More
29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

Know More
27 APR
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

Know More
22 APR
NodeJS Development (Classroom)

11:00 AM-12:30 PM

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

8 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

03:30 PM-05:30 PM

24 FEB
Angular 2 with Type Script (Online)

09:00 PM-11:00 PM IST(Mon,Wed,Fri)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+