MEDIATOR PATTERN

Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Mediator design pattern is used to provide a centralized communication medium between different objects in a system.

Rather than interacting directly with each other, objects ask the Mediator to interact on their behalf which results in reusability and loose coupling. It encapsulates the interaction between the objects and makes them independent from each other. This allows them to vary their interaction with other objects in a totally different way by implementing a different mediator. The Mediator helps to reduce the complexity of the classes. Each object no longer has to know in detail about how to interact with the other objects. The coupling between objects goes from tight and brittle to loose and agile.

Mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly, the system components are tightly-coupled with each other that make maintainability cost higher and not flexible to extend easily. Mediator pattern focuses on providing a mediator between objects for communication and help in implementing lose-coupling between objects.

Air traffic controller is a great example of mediator pattern where the airport control room works as a mediator for communication between different flights. Mediator works as a router between objects and it can have its own logic to provide way of communication.

The major participants of the Mediator Pattern are:

  • Mediator: Defines an interface for communicating with Colleague objects.
  • ConcreteMediator: Implements co-operative behavior by coordinating Colleague objects. It also knows and maintains its colleagues.
  • Colleague Classes: Each Colleague class knows its Mediator objects. Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.

Example:

package com.myjavablog.behavioural.mediator;

public interface IMediator {

public void talk();
public void fight();
public void registerA(ColleagueA a);
public void registerB(ColleagueB b);
}

package com.myjavablog.behavioural.mediator;

public class ConcreteMediator implements IMediator {

ColleagueA talk;
ColleagueB fight;

@Override
public void talk() {
System.out.println("Mediator is talking");
//Talk colleague doing some stuff
}

@Override
public void fight() {
System.out.println("Mediator is fighting");
//Fight colleague doing some stuff
}

@Override
public void registerA(ColleagueA a) {
this.talk =a;
}

@Override
public void registerB(ColleagueB b) {
this.fight =b;
}
}

package com.myjavablog.behavioural.mediator;

public abstract class Colleague {
IMediator mediator;
public abstract void doSomething();
}

package com.myjavablog.behavioural.mediator;

public class ColleagueA extends Colleague{

ColleagueA(IMediator mediator){
this.mediator = mediator;
this.mediator.registerA(this);
}

@Override
public void doSomething() {
this.mediator.talk();
}
}

package com.myjavablog.behavioural.mediator;

public class ColleagueB extends Colleague{

ColleagueB(IMediator mediator){
this.mediator = mediator;
this.mediator.registerB(this);
}

@Override
public void doSomething() {
this.mediator.fight();
}
}

package com.myjavablog.behavioural.mediator;

public class MediatorPatternTest {

public static void main(String[] args) {

IMediator mediator = new ConcreteMediator();

ColleagueA talkColleague = new ColleagueA(mediator);
talkColleague.doSomething();

ColleagueB fightColleague = new ColleagueB(mediator);
fightColleague.doSomething();

}
}

Output:

Mediator is talking
Mediator is fighting

 

When to use the Mediator Pattern:

  • A set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand.
  • Reusing an object is difficult because it refers to and communicates with many other objects.
  • A behavior that’s distributed between several classes should be customizable without a lot of sub-classing.

 

Mediator Pattern in JDK:

  • java.util.Timer class scheduleXXX () methods.
  • java Concurrency Executor execute () method.
  • java.lang.reflect.Method invoke () method.

 

 

Leave a Comment

Bitnami