Today I will be discussing Mediator Pattern.Let me explain it with an example.
Suppose me and my friend got into a problem and we decided to not communicate with each other directly.In this case we will use the class monitor as a Mediator.
So whatever message i want to convey to my friend i will do it using Monitor and similarly my friend will also use this method.This signifies that there will be no direct communication-only using the Mediator.
Uses of Mediator Pattern(In terms of Software):
Suppose there is a dialog box in which a lot of controls are there and there are many rules like if a checkbox is checked then some input edit text will be disabled, if a specific radio button is clicked some controls will be disabled e.t.c.Instead of all these controls communicating with each other directly we keep the logic of all those conditions in the Mediator and the controls talk to each other through this Mediator.
from abc import ABC,abstractmethod
class ClassMonitor(ABC):
@abstractmethod
def notify(self,sender,event):
pass
class ConcreteMonitor(ClassMonitor):
def __init__(self,student1,student2):
self.student1 = student1
self.student2 = student2
def notify(self,sender,event):
if(event == "A"):
print("Monitor has reacted to event A ")
self.student2.do_b()
elif(event == "D"):
print("Monitor has reacted to event D")
self.student1.do_c()
class BaseStudent(ABC):
def __init__(self,monitor):
self._monitor = monitor
def setMonitor(self,monitor):
self._monitor = monitor
def getMonitor(self):
return self._monitor
class Ridit(BaseStudent):
def do_a(self):
print("Ridit does A")
self.getMonitor().notify(self,"A")
def do_c(self):
print("Ridit does C")
class Rajdeep(BaseStudent):
def do_d(self):
print("Rajdeep does D")
self.getMonitor().notify(self,"D")
def do_b(self):
print("Rajdeep does B")
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
ridit = Ridit(None)
rajdeep = Rajdeep(None)
classMonitor = ConcreteMonitor(ridit,rajdeep)
ridit.setMonitor(classMonitor)
rajdeep.setMonitor(classMonitor)
ridit.do_a()
rajdeep.do_d()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
public interface ClassMonitor {
void notify(BaseStudent sender,String event);
}
public class ConcreteClassMonitor implements ClassMonitor {
private Ridit student1;
private Rajdeep student2;
public ConcreteClassMonitor(Ridit student1,Rajdeep student2) {
this.student1 = student1;
this.student2 = student2;
}
@Override
public void notify(BaseStudent sender, String event) {
// TODO Auto-generated method stub
if(event.equals("A")) {
System.out.println("Monitor has reacted to event A ");
this.student2.do_b();
}
if(event == "D"){
System.out.println("Monitor has reacted to event D");
this.student1.do_c();
}
}
}
public abstract class BaseStudent {
private ClassMonitor monitor;
public BaseStudent(ClassMonitor monitor) {
this.monitor = monitor;
}
public ClassMonitor getMonitor() {
// TODO Auto-generated method stub
return this.monitor;
}
public void setMonitor(ClassMonitor monitor) {
// TODO Auto-generated method stub
this.monitor = monitor;
}
}
public class Ridit extends BaseStudent {
public Ridit(ClassMonitor monitor) {
super(monitor);
// TODO Auto-generated constructor stub
}
public void do_a() {
System.out.println("Ridit doing a");
this.getMonitor().notify(this,"A");
}
public void do_c() {
System.out.println("Ridit doing c");
this.getMonitor().notify(this,"C");
}
}
public class Rajdeep extends BaseStudent {
public Rajdeep(ClassMonitor monitor) {
super(monitor);
// TODO Auto-generated constructor stub
}
public void do_d() {
System.out.println("Rajdeep doing d");
this.getMonitor().notify(this,"D");
}
public void do_b() {
System.out.println("Rajdeep doing b");
this.getMonitor().notify(this,"B");
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ridit ridit = new Ridit(null);
Rajdeep rajdeep = new Rajdeep(null);
ClassMonitor classmonitor = new ConcreteClassMonitor(ridit, rajdeep);
ridit.setMonitor(classmonitor);
rajdeep.setMonitor(classmonitor);
ridit.do_a();
rajdeep.do_d();
}
}
/*
* ClassMonitor.h
*
* Created on: 13-Jan-2024
* Author: ridit
*/
#ifndef CLASSMONITOR_H_
#define CLASSMONITOR_H_
#include <iostream>
#include "BaseStudent.h"
using namespace std;
class BaseStudent;
class ClassMonitor {
public:
virtual void notify(BaseStudent* sender,string event) = 0;
};
#endif /* CLASSMONITOR_H_ */
/*
* ConcreteClassMonitor.h
*
* Created on: 13-Jan-2024
* Author: ridit
*/
#ifndef CONCRETECLASSMONITOR_H_
#define CONCRETECLASSMONITOR_H_
#include "BaseStudent.h"
#include "ClassMonitor.h"
#include "Ridit.h"
#include "Baba.h"
class ConcreteClassMonitor :public ClassMonitor{
private:
Ridit* student1;
Baba* student2;
public:
ConcreteClassMonitor(Ridit* student1,Baba* student2) {
this->student1 = student1;
this->student2 = student2;
}
virtual ~ConcreteClassMonitor(){
}
void notify(BaseStudent* sender, string event) {
if(event == "A") {
cout << "monitor has reacted to event A" << endl;
student2->do_b();
}
if(event == "D") {
cout << "monitor has reacted to event D" << endl;
student1->do_c();
}
}
};
#endif /* CONCRETECLASSMONITOR_H_ */
/*
* BaseStudent.h
*
* Created on: 13-Jan-2024
* Author: ridit
*/
#ifndef BASESTUDENT_H_
#define BASESTUDENT_H_
#include "ClassMonitor.h"
class BaseStudent {
private:
ClassMonitor* monitor;
public:
BaseStudent(ClassMonitor* monitor) {
this->monitor = monitor;
}
ClassMonitor* getMonitor() {
return this->monitor;
}
void setMonitor(ClassMonitor* monitor) {
this->monitor = monitor;
}
};
#endif /* BASESTUDENT_H_ */
/*
* Ridit.h
*
* Created on: 13-Jan-2024
* Author: ridit
*/
#ifndef RIDIT_H_
#define RIDIT_H_
#include <iostream>
#include "BaseStudent.h"
#include "ClassMonitor.h"
using namespace std;
class Ridit: public BaseStudent {
public:
Ridit(ClassMonitor* monitor) : BaseStudent(monitor){
}
virtual ~Ridit(){
}
void do_a() {
cout << "Ridit doing a" << endl;
this->getMonitor()->notify(this, "A");
}
void do_c() {
cout << "Ridit doing c" << endl;
this->getMonitor()->notify(this, "C");
}
};
#endif /* RIDIT_H_ */
/*
* Baba.h
*
* Created on: 13-Jan-2024
* Author: Ridit
*/
#ifndef BABA_H_
#define BABA_H_
#include <iostream>
#include "BaseStudent.h"
#include "ClassMonitor.h"
using namespace std;
class Baba: public BaseStudent {
public:
Baba(ClassMonitor* monitor) : BaseStudent(monitor){
}
virtual ~Baba(){
}
void do_b() {
cout << "Baba doing b" << endl;
this->getMonitor()->notify(this, "B");
}
void do_d() {
cout << "Baba doing d" << endl;
this->getMonitor()->notify(this, "D");
}
};
#endif /* BABA_H_ */
//============================================================================
// Name : MediatorPattern.cpp
// Author : Ridit
// Version :
// Copyright : @Ridit...
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "Ridit.h"
#include "Baba.h"
#include "ConcreteClassMonitor.h"
#include "ClassMonitor.h"
using namespace std;
int main() {
Ridit* ridit = new Ridit(NULL);
Baba* baba = new Baba(NULL);
ClassMonitor* monitor = new ConcreteClassMonitor(ridit, baba);
ridit->setMonitor(monitor);
baba->setMonitor(monitor);
ridit->do_a();
baba->do_d();
}