Saturday 1 October 2022

 Design Pattern: Mediator

a)Mediator Design Pattern:

In mediator pattern,  we develop as class which act as coupling among different  concrete classes. It can look like Facade or bridge design pattern at times, but difference lies in it that it calls object of different classes and their methods as per need.


E.g.: We want to build a bottle filling plant. 

The processes in it are: 

a) Create a bottle,

b) Fill it with juice,

c) Put air-tight cap on it,

d) Put label,

e) Dispatch it

1) Create multiple classes for functionality:

a) public class Bottle(){

  public String createBot(String message){

System.out.println(message);

}

}

b) public class FillJuice {

int processValue;

print int processParam(){

int formula = (3.14*7.9(12)+12*34);

formula = processValue;

return formula;

}

  public String fill(){

System.out.println("Fill juice in bottle with process prameter: " + processValue);

}

}

c) public class BottlePack{

  public String pack(){

  System.out.println("Bottle capped, labelled & ready for dispatch");

}

}

//Create concrete class which calls all the above classes and executes the bottle filling process

d) public class BottleFill {

Bottle bottle = new Bottle();

FillJuice fillJuice = new FillJuice();

BottlePack bottlePack = new BottlePack();

bottle.createBot("Create a glass bottle for juice");

fillJuice.fill();

bottlePack.pack();

}


No comments:

Post a Comment