Mediator Pattern

Introduction and Implementation of Mediator Design Pattern


Category: Design Patterns Tags: C#

Mediator Pattern - Code Files

Introduction

    Mediator design pattern is a Behavioral Design Pattern. Mediator pattern is used to create more decoupled system by removing direct references of classes instead using a mediator. Mediator encapsulates all the required classes so only a mediator can be referenced instead of multiple classes making code more clean and decoupled.

Think of an authenticator class which sends notification to user via email and SMS every time a login is successful. Now to send notifications we need to reference EmailNotifier, SMSNotifier etc into authenticator class. To avoid direct communication between classes we can create a mediator class which can sit between authenticator and notifier classes.

Implementation

    Lets create notifier classes as following.

public interface IEmailNotifier
{
    public void Notify(string email);
}

public class EmailNotifier : IEmailNotifier
{
    public void Notify(string email) 
    {
        Console.WriteLine($"Email sent to {email}");
    }
}
public interface ISMSNotifier
{
    public void Notify(string phoneNumber);
}

public class SMSNotifier : ISMSNotifier
{
    public void Notify(string phoneNumber)
    {
        Console.WriteLine($"SMS sent to {phoneNumber}");
    }
}

We can create DB context class which can fetch user phone number using email from database.

public interface IUserDBContext
{
    string GetUserPhoneNumber(string email);
}

public class UserDBContext : IUserDBContext
{
    public string GetUserPhoneNumber(string email)
    {
        return "9090909090";
    }
}

If we reference all above classes in authenticator that will make system tightly coupled. Lets create a mediator first.

public interface IMediator
{
    void Send(string email);
}

public class NotificationHandler : IMediator
{
    private readonly IEmailNotifier emailNotifier;
    private readonly ISMSNotifier smsNotifier;
    private readonly IUserDBContext userDBContext;

    public NotificationHandler()
    {
        emailNotifier = new EmailNotifier();
        smsNotifier = new SMSNotifier();
        userDBContext = new UserDBContext();
    }

    public void Send(string email)
    {
        emailNotifier.Notify(email);
        var phoneNumber = userDBContext.GetUserPhoneNumber(email);
        smsNotifier.Notify(phoneNumber);
    }
}

Now we can reference this mediator in Authenticator.

public interface IAuthenticator
{
    bool Login(string email, string password);
}

public class Authenticator : IAuthenticator
{
    private readonly IMediator _mediator;

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

    public bool Login(string email, string password)
    {
        Console.WriteLine("Authenticated successfully");
        _mediator.Send(email);
        return true;
    }
}

Lets run above code.

IMediator mediator = new NotificationHandler();
IAuthenticator authenticator = new Authenticator(mediator);
authenticator.Login("abc@dotnetlovers.com", "password");

Output

Authenticated successfully

Email sent to abc@dotnetlovers.com

SMS sent to 9090909090

 


Like 0 People
Last modified on 25 October 2022
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 164
Questions: 16
Given Best Solutions: 16 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x