Explanation of Mediator Design Pattern

Explanation of Mediator Design Pattern

Hello everyone, in this article we are going to talk about Mediator Desgin within behavioral Design Patterns. I will make a simple explanation and then we will make an example about it with C#.

Let's Begin.

Generally we use complex components and structures in our projects. When we want these components to make something, we generally call just a method and it probably fires up very complex works. We do not know what is happening at background we just know our method did something.

We can say the communications between GUI components of a program is an example for Mediator. Any of GUI components do not know what the other components do, and they just make them do instructed tasks.

Now we are going to make an example about Mediator Design Pattern and we will understand what it is.

In our example we are going to make a computer example. When we power button of the computer, simply BIOS will be started first. And then BIOS will check the CPU, RAM Graphic Card and HDD for they are installed and ready.

Below image you can see the schematic of our example:

Mediator Design Pattern Schematic

If BIOS and Input/Outputs are ready BIOS will call operating system. As a result our computer will be started.

Let's write some codes:

We are going to derive same types classes from this interface. We define the classes which are delegate inputs and outputs inside a computer. All IO units will be initialized and return the initialization status.

IO.cs


namespace MediatorPattern_Example
{
    //We are going to derive same types classes from this interface.
    //We define the classes which are delegate inputs and outputs inside a computer.
    //All IO units will be initialized and return the initialization status.
    interface IO
    {
        void initialize();
        bool getStatus();
    }
}

CPU.cs


using System;
using System.Threading;

namespace MediatorPattern_Example
{
    class CPU : IO
    {
        private bool isReady = false;

        public bool getStatus() => this.isReady;
        
        public void initialize()
        {
            Console.WriteLine("Checking CPU...");
            Thread.Sleep(100);
            this.isReady = true;
            Console.WriteLine("CPU is ready...");
            Console.WriteLine("--------------------------");
        }
    }
}

RAM.cs


using System;
using System.Threading;

namespace MediatorPattern_Example
{
    class RAM : IO
    {
        private bool isReady = false;

        public bool getStatus() => this.isReady;

        public void initialize()
        {
            Console.WriteLine("Checking System Memory...");
            Thread.Sleep(100);
            this.isReady = true;
            Console.WriteLine("System Memory is ready...");
            Console.WriteLine("--------------------------");
        }
    }
}

HDD.cs


using System;
using System.Threading;

namespace MediatorPattern_Example
{
    class HDD : IO
    {
        private bool isReady = false;

        public bool getStatus() => this.isReady;
        public void initialize()
        {
            Console.WriteLine("Checking Hard Drives...");
            Thread.Sleep(100);
            this.isReady = true;
            Console.WriteLine("Hard Drives are ready...");
            Console.WriteLine("--------------------------");
        }
    }
}

OS.cs


using System;

namespace MediatorPattern_Example
{
    class OS : IO
    {
        private bool isReady = false;

        public bool getStatus() => this.isReady;

        public void initialize()
        {
            Console.WriteLine("Operating System is starting...");
            this.isReady = true;
        }
    }
}
This BIOS class will be our Mediator class. BIOS is going to coordinate everything when we press the power button It will first check the IO Parts and then if there is no problem call the operating system. First start all of IO's and if there is no problem with the IO's then we are good to call operating system.

BIOS.cs


using System;

namespace MediatorPattern_Example
{
    //This BIOS class will be our Mediator class.
    //BIOS is going to coordinate everything when we press the power button
    //It will first check the IO Parts and then if there is no problem 
    //call the operating system.
    public class BIOS
    {
        public void pressPowerButton()
        {
            //First start all of IO's
            CPU _cpu = new CPU();
            _cpu.initialize();
            if (_cpu.getStatus() == false)
                Console.WriteLine("Starting CPU is failed...");

            RAM _ram = new RAM();
            _ram.initialize();
            if (_ram.getStatus() == false)
                Console.WriteLine("Starting RAM is failed...");

            HDD _hdd = new HDD();
            _hdd.initialize();
            if (_hdd.getStatus() == false)
                Console.WriteLine("Starting HDD is failed...");

            //if there is no problem with the IO's then we are good to call operating system
            if (_cpu.getStatus() && _ram.getStatus() && _hdd.getStatus())
            {
                OS _os = new OS();
                _os.initialize();
                if (_os.getStatus() == true)
                    Console.WriteLine("Operating System started successfully...");
            }
        }
    }
}
Main program have no idea what is under the hood. It just instructs the mediator class which is BIOS class in this example to start the system. All of the tasks will be performed via BIOS class.

Program.cs


using System;

//Main program have no idea what is under the hood.
//It just instructs the mediator class which is BIOS class in this example to start the system.
//All of the tasks will be performed via BIOS class.
namespace MediatorPattern_Example
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.Title = "Mediator Design Pattern Example - TheCodeProgram";

            //All we need to do from our main program to press the power button
            BIOS _bios = new BIOS();
            _bios.pressPowerButton();

            Console.ReadLine();
        }
    }
}

Our example is ready. You can see the output of the program at below image :

Mediator Design Pattern Example Output

Additional Information:

If you are working with Object Orientated Programming, you probably heard about MVC which stands for Model-View-Controller. We can say this Mediator Design pattern is the Controller part of the MVC. Because it is coordination the communication between Models and Views.

That is all in this article.

You can find the example application on Github via : https://github.com/thecodeprogram/MediatorPattern_Example

Have a good Mediating the classes.

Burak Hamdi TUFAN.


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...