The Mediator Pattern in Java

Introduction to the Mediator Pattern

Design pattern in a simple meaning, is a way to design reusable object-oriented code. We can think of ways to design our classes, interfaces, enums and their members so that the code is reusable but flexible.

There are 23 most important design patterns, pioneered by the book Design Patterns: Elements of Reusable Object-Oriented Software, written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides or Gang of Four for short. We will cover these design pattern one by one.

The Mediator pattern defines an object that encapsulates how a set of objects interact. Mediator pattern promotes loose coupling by keeping objects from referring to each other explicitly, and it lets us vary their interaction independently. The Mediator pattern can be easy to understand if we think of a group chat. Each user just sends the message to the Mediator, the Mediator will find all other users in the group and send that message to them all.

Implementation of Meditator Pattern in Java

First we create a User.java in the src folder. It is an abstract class for the users of the chat group.

public abstract class User {
  String name;
  public User(String name) {
    this.name = name;
  }
  abstract void send(String msg);
  abstract void receive(String msg);
}

Next we create a Mediator interface, this will be the base for the Mediator:

public interface Mediator {
  void sendMessage(String message, User user);
  void addUser(User user);
}

Now, we create a GroupUser class, it is a concrete class that extends the User class. This class compose the Mediator class:

public class GroupUser extends User{
  protected Mediator med;
  public GroupUser(String user, Mediator med) {
    super(user);
    this.med = med;
  }
  void send(String msg) {
    med.sendMessage(msg, this);
  }
  void receive(String msg) {
    System.out.println("User " + name + 
        " receives : " + msg);
  }
}

And then the GroupChat class, which is the concrete Mediator class:

import java.util.*;
public class GroupChat implements Mediator{
  List<User> users = new ArrayList<>();
  public GroupChat(String groupName){
    System.out.println("Group " + groupName
        + " created");
  }
  public void addUser(User user) {
    users.add(user);
    System.out.println("User " + user.name
        + " joins group chat");
  }
  public void sendMessage(String msg, User user){
    System.out.println("User " + user.name
        + " sends : " + msg);
    users.stream()
        .filter(groupUser->
            !groupUser.name.equals(user.name))
        .forEach(groupUser->
            groupUser.receive(msg));
  }
}

And finally in Test.java:

public class Test {
  public static void main(String[] args){
    //create a chat group named Test
    GroupChat group = new GroupChat("Test");
    //create some group users in this group
    User user01 = new GroupUser("Pavatil", group);
    User user02 = new GroupUser("Prisha", group);
    User user03 = new GroupUser("Krisha", group);
    //add user to the group
    group.addUser(user01);
    group.addUser(user02);
    group.addUser(user03);
    //let user01 send a message
    user01.send("Hello to all");
  }
}

Now run the code and we have the output:

Group Test created
User Pavatil joins group chat
User Prisha joins group chat
User Krisha joins group chat
User Pavatil sends : Hello to all
User Prisha receives : Hello to all
User Krisha receives : Hello to all

Leave a Reply