Easy Tutorial
❮ Iterator Pattern Service Locator Pattern ❯

Mediator Pattern

The Mediator Pattern is used to reduce the complexity of communication between multiple objects and classes. This pattern provides a mediator class that typically handles communication between different classes and supports loose coupling, making the code easier to maintain. The Mediator Pattern is a behavioral pattern.

Introduction

Intent: Encapsulate a series of object interactions with a mediator object. The mediator ensures that objects do not need to explicitly reference each other, thus allowing for loose coupling and the ability to independently change their interactions.

Main Solution: When objects are heavily interconnected, the system's structure becomes complex. If an object changes, it requires tracking and corresponding changes in related objects.

When to Use: When multiple classes are coupled in a mesh-like structure.

How to Solve: Transform the mesh structure into a star structure.

Key Code: Communication between Colleague objects is encapsulated in a separate class for handling.

Examples:

  1. Before joining the WTO, countries traded with each other directly, leading to a complex structure. Now, countries trade with each other through the WTO.
  2. Airport traffic control systems.
  3. MVC frameworks, where the Controller (C) acts as the mediator between the Model (M) and View (V).

Advantages:

  1. Reduces class complexity by converting many-to-many relationships into one-to-one.
  2. Decouples classes.
  3. Adheres to the Law of Demeter.

Disadvantages: The mediator can become large and complex, making it difficult to maintain.

Usage Scenarios:

  1. When objects in the system have complex reference relationships, leading to a chaotic dependency structure and difficulty in reusing objects.
  2. When you want to encapsulate behaviors from multiple classes through an intermediate class without creating too many subclasses.

Caution: Should not be used when responsibilities are unclear.

Implementation

We demonstrate the Mediator Pattern using a chat room example. In this example, multiple users can send messages to the chat room, which displays the messages to all users. We create two classes, ChatRoom and User. The User object uses ChatRoom methods to share their messages.

MediatorPatternDemo, our demonstration class, uses User objects to show their communication.

Step 1

Create the mediator class.

ChatRoom.java

import java.util.Date;

public class ChatRoom {
   public static void showMessage(User user, String message){
      System.out.println(new Date().toString()
         + " [" + user.getName() +"] : " + message);
   }
}

Step 2

Create the user class.

User.java

public class User {
   private String name;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public User(String name){
      this.name  = name;
   }

   public void sendMessage(String message){
      ChatRoom.showMessage(this, message);
   }
}

Step 3

Use User objects to show their communication.

MediatorPatternDemo.java

public class MediatorPatternDemo {
   public static void main(String[] args) {
      User robert = new User("Robert");
      User john = new User("John");

      robert.sendMessage("Hi! John!");
      john.sendMessage("Hello! Robert!");
   }
}

Step 4

Execute the program, output result:

Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John!
Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!
❮ Iterator Pattern Service Locator Pattern ❯