Mediator Pattern in Java

A program is considered to be efficient if it has less coupling between its software module. However, this does not mean that objects should not have the necessary interactions between them. Often situations arise when objects have to be tightly coupled so that fast interaction between them is possible. The mediator pattern comes into play here. It reduces complexity and coupling between such objects by creating a mediator object that will be responsible for interactions. The mediator pattern comes under behavioral design patterns as it governs how an object will behave and the consequent information flow between them.


The Mediator Pattern is defined as a behavioral design pattern that is responsible for reducing coupling between already tightly coupled objects by creating a mediator object through which indirect communication will take place between objects.

Advantage of Using Mediator Pattern

  • As this pattern reduces coupling, it decreases the chances of a complete code fall-through because of an error in one of the sections of the code.
  • It also makes sure the code is readable and reusable if changes are required form time to time.

Example and Code

In this example, we will implement a simple rice cooker application that will test the water level. Before starting the cooker, it will check for water and before turning it off, it will check whether all the water has evaporated.

public class Mediator {
private Switch switch;
private Cooker cooker;
private WaterLevel water;

public void startCooker() {
water.present(); }

public void stopCooker() {
water.absent(); }

//Now we will be editing the Swtich and cooker classes

public class Switch {
private Mediator mediator;
public void startCooker() {

public class Cooker {
private Mediator mediator;
private Boolean water = false;

public void present() {
water = true;

public void absent() {
water = false;

When to Use the Mediator Pattern

This pattern finds the best use when you want to limit the number of subclasses that a superclass can have. A functionality that is normally linked between several objects will become localized to the mediator object after this pattern is implanted. A certain disadvantage that this pattern might bring with it is a complex mediator object if several objects are dependent on it for communication ties.


  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.