Mediator — Behavioral Design Pattern

The mediator design pattern is very useful when building complex application, which interacts with multiple layers and objects in a system. By using this pattern, the tight coupling in such situations can be reduced and higher maintainability with extendibility can be gained.

Real World Example

Let’s think about a classic example of an air traffic control system.

In this system, the Air Traffic Control (ATC) unit. which located in different geographical locations to communicate with each of these aircrafts. And control their routes depending on the on going air traffic throughout their journey. So, the ATC unit act as the common mediator for all of these aircrafts.

In aerospace, there can be specializations depending on the type of the aircraft. Ex : passenger aircraft, military aircraft or cargo aircraft, etc... For those, a dedicated / specialized ATC units are there to coordinate with each other. So that can be taken as a special mediator, which is a extended version of the common mediator.

Mediator Pattern in UML Diagram

In the above diagram, each of these items can be identified as;

  • Mediator : declares the methods to communicate with each dependent components.
  • Component : each of these independent entities can perform their own tasks without having any dependency with other component types.
  • Concrete Mediator : holds the links between other components to coordinate with each other.

Code Example

We’ll take a sample scenario of a remote controller, where it has multiple buttons to control TV based on the user inputs. In that case, the remote’s micro controller can keep current states of the buttons used by the user. likewise, it can trigger volume down when you click on the menu button to open-up the TV’s central menu. So, this controller behavior is demonstrated in the below sample code with the use of mediator design pattern.

The following program is a JAVA console based application.

  1. Create Mediator interface.
/**
* Mediator interface
*/
public interface ActionMediator {

/**
* Setter for component
*
*
@param button - the component
*/
void add(Button button);

/**
* Common action for all components to communicate with
*
*
@param type - the button type
*/
void notify(ButtonType type);
}

2. Create Concrete Mediator class.

public class ActionMediatorImpl implements ActionMediator {

private Button powerButton;
private Button volumeButton;
private Button menuButton;

@Override
public void add(Button button) {
switch (button.getType()) {
case POWER:
this.powerButton = button;
break;
case VOLUME:
this.volumeButton = button;
break;
case MENU:
this.menuButton = button;
}
}

@Override
public void notify(ButtonType type) {
switch (type) {
case POWER:
this.powerButton.toggleState();
break;
case VOLUME:
this.volumeButton.toggleState();
break;
case MENU:
this.menuButton.toggleState();
}
}
}

3. Create Abstract component class as the component base.

/**
* Abstract class for base component
*/
public abstract class Button {

protected ActionMediator mediator;
protected ButtonType type;

public Button(ActionMediator mediator, ButtonType type) {
this.mediator = mediator;
this.type = type;
}

/**
* Common action
*/
public abstract void press();

/**
* Toggle state
*/
public abstract void toggleState();

public ButtonType getType() {
return type;
}
}

4. Create individual component classes. Ex: MenuButton

/**
* Menu Button implementation
*/
public class MenuButton extends Button {

private boolean clicked = false;

public MenuButton(ActionMediator mediator) {
super(mediator, ButtonType.MENU);
}

@Override
public void press() {
System.out.println("MenuButton : press");
// do menu button related logic
// in the same time, save other action states in the remote's memory
this.mediator.notify(ButtonType.VOLUME);
this.mediator.notify(ButtonType.POWER);
}

@Override
public void toggleState() {
System.out.println("MenuButton : toggleState");
this.clicked = !this.clicked;
}
}

The application’s console output.

MenuButton : press
VolumeButton : toggleState
PowerButton : toggleState

Here’s the link for the source code.

I hope you liked it and got clear understanding about the mediator design pattern.

Thanks for reading !!…

Full Stack Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

WriteUpp Web v2.13.5 — Flexibility around “locking” of notes

Interpreting Fibonacci series in Python

Best Free WordPress Search Plugins List

Send JSON data to Firebase Firestore database

NODMCU Esp8266–12 USB CP2102

How to host a website on an AWS S3 bucket using Route 53

AMA Summary with Infinity Gainz

Data Driven Actions with an Integrated Architecture in Power Platform

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Buwaneka Boralessa

Buwaneka Boralessa

Full Stack Engineer

More from Medium

Design Patterns in JAVA

SOLID Principles in Java

SOLID principles: The Definitive Guide (Part I)