Design Pattern: Mediator Pattern in Java

Mediator pattern is used to restrict direct interaction between objects. A mediator class works as the middleman/communicator when one object wants to connect/use another object.

Mediator Pattern communication Between the Mediator and Colleagues

This article demonstrates Mediator pattern implementations in Java. Check the following examples.

Examples

Here are examples of Mediator pattern implementation-

Example #1: General Mediator

Let’s consider a general example.

Class Diagram

Take a look at the class diagram.

Mediator Interface

// IMediator.java

package com.bigboxcode.designpattern.mediator.mediator;

public interface IMediator {
    void sendMessage(Colleague colleague, String msg);
}

Mediator Class

// Mediator.java

package com.bigboxcode.designpattern.mediator.mediator;

public class Mediator implements IMediator {

    @Override
    public void sendMessage(Colleague receiver, String msg) {
        receiver.receiveMessage(msg);
    }

}

Colleague Abstract Class

// Colleague.java

package com.bigboxcode.designpattern.mediator.mediator;

public abstract class Colleague {
    protected Mediator mediator;

    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }

    public abstract void sendMessage(Colleague colleague, String message);

    public abstract void receiveMessage(String message);
}

Colleague1 Class

// Colleague1.java

package com.bigboxcode.designpattern.mediator.mediator;

public class Colleague1 extends Colleague{
    public Colleague1(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void sendMessage(Colleague colleague, String msg) {
        mediator.sendMessage(colleague, msg);
    }

    @Override
    public void receiveMessage(String msg) {
        System.out.println("Message received in Colleague1: " + msg);
    }
}

Colleague2 Class

// Colleague2.java

package com.bigboxcode.designpattern.mediator.mediator;

public class Colleague2 extends Colleague {
    public Colleague2(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void sendMessage(Colleague colleague, String msg) {
        mediator.sendMessage(colleague, msg);
    }

    @Override
    public void receiveMessage(String msg) {
        System.out.println("Message received in Colleague2: " + msg);
    }
}

Colleague3 Class

// Colleague3.java

package com.bigboxcode.designpattern.mediator.mediator;

public class Colleague3 extends Colleague {
    public Colleague3(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void sendMessage(Colleague colleague, String msg) {
        mediator.sendMessage(colleague, msg);
    }

    @Override
    public void receiveMessage(String msg) {
        System.out.println("Message received in Colleague3: " + msg);
    }
}

Demo

// Demo.java

package com.bigboxcode.designpattern.mediator.mediator;

public class Demo {
    public static void main(String[] args) {
        Mediator mediator  = new Mediator();

        Colleague1 colleague1 = new Colleague1(mediator);
        Colleague2 colleague2 = new Colleague2(mediator);
        Colleague3 colleague3 = new Colleague3(mediator);

        colleague1.sendMessage(colleague2, "message from colleague1");

        System.out.println("-----------------------------------------------");

        colleague1.sendMessage(colleague3, "message from colleague1");

        System.out.println("-----------------------------------------------");

        colleague2.sendMessage(colleague3, "message from colleague2");

        System.out.println("-----------------------------------------------");

        colleague3.sendMessage(colleague1, "message from colleague3");

        System.out.println("-----------------------------------------------");

    }
}

Output

Message received in Colleague2: message from colleague1
-------------------------------------------------------------------------
Message received in Colleague3: message from colleague1
-------------------------------------------------------------------------
Message received in Colleague3: message from colleague2
-------------------------------------------------------------------------
Message received in Colleague1: message from colleague3
-------------------------------------------------------------------------

Source Code

Use the following link to get the source code:

Other Code Implementations

Use the following links to check Mediator pattern implementation in other programming languages.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.