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.
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.
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:
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;
}
}
}
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...");
}
}
}
}
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();
}
}
}
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.
Comments