Full Stack • Java • System Design • Cloud • AI Engineering

Mediator Design Pattern in Java

Learn Mediator Design Pattern in Java with chat room examples, air traffic control systems, Spring events, enterprise workflow communication, UML diagrams, code examples, and interview questions.

What You Will Learn

  • What is Mediator Pattern?
  • Why Mediator Pattern is Needed
  • Object Communication Problems
  • Chat Room Example
  • Air Traffic Control Example
  • Java Implementation
  • Spring Event Examples
  • Enterprise Workflow Examples
  • Benefits and Limitations
  • Interview Questions

Introduction

Imagine an enterprise application with multiple services:

Order Service

Payment Service

Inventory Service

Shipping Service

Notification Service

Without proper design:

Order Service talks to Payment

Payment talks to Inventory

Inventory talks to Shipping

Shipping talks to Notification

Every service knows about every other service.

This creates:

Tight Coupling

Complex Dependencies

Hard Maintenance

Mediator Pattern solves this problem.


What is Mediator Pattern?

Mediator is a Behavioral Design Pattern that defines an object that coordinates communication between multiple objects.

Instead of objects talking directly:

Object A → Object B

Object B → Object C

Object C → Object D

All communication goes through:

Mediator

Purpose of Mediator Pattern

Primary goal:

Reduce Direct Dependencies

Between Objects

Real World Analogy

Airport Control Tower.

Pilots do NOT communicate directly.

Instead:

Pilot A

↓

Control Tower

↓

Pilot B

The control tower acts as a Mediator.


Problem Without Mediator

flowchart LR
    A[Service A]
    B[Service B]
    C[Service C]
    D[Service D]

    A --> B
    A --> C
    A --> D
    B --> C
    B --> D
    C --> D

Many connections.

Very difficult to maintain.


Solution With Mediator

flowchart LR
    A[Service A]
    B[Service B]
    C[Service C]
    D[Service D]
    M[Mediator]

    A --> M
    B --> M
    C --> M
    D --> M

Only one communication channel.


Mediator Architecture

flowchart LR
    A[Colleague A]
    B[Colleague B]
    C[Colleague C]
    M[Mediator]

    A --> M
    B --> M
    C --> M

Key Components

Mediator

Defines communication rules.


Concrete Mediator

Implements communication behavior.


Colleague

Objects that communicate through mediator.


Concrete Colleague

Actual participating objects.


UML Diagram

classDiagram

class Mediator {
    +send()
}

class ConcreteMediator

class User

Mediator <|.. ConcreteMediator

ConcreteMediator --> User

Chat Room Example

Without mediator:

User1 → User2

User1 → User3

User2 → User3

Many connections.


With Mediator

flowchart LR
    A[User1]
    B[User2]
    C[User3]
    D[ChatRoom]

    A --> D
    B --> D
    C --> D

All communication flows through ChatRoom.


Step 1: Mediator Interface

public interface ChatMediator {

    void sendMessage(
            String message,
            User user);
}

Step 2: User Class

public abstract class User {

    protected ChatMediator mediator;

    protected String name;

    public User(
            ChatMediator mediator,
            String name) {

        this.mediator = mediator;
        this.name = name;
    }

    public abstract void send(
            String message);

    public abstract void receive(
            String message);
}

Step 3: Concrete User

public class ChatUser
        extends User {

    public ChatUser(
            ChatMediator mediator,
            String name) {

        super(mediator, name);
    }

    @Override
    public void send(
            String message) {

        mediator.sendMessage(
                message,
                this);
    }

    @Override
    public void receive(
            String message) {

        System.out.println(
                name + " received: "
                        + message);
    }
}

Step 4: Concrete Mediator

import java.util.ArrayList;
import java.util.List;

public class ChatRoomMediator
        implements ChatMediator {

    private List<User> users =
            new ArrayList<>();

    public void addUser(
            User user) {

        users.add(user);
    }

    @Override
    public void sendMessage(
            String message,
            User sender) {

        for(User user : users) {

            if(user != sender) {

                user.receive(message);
            }
        }
    }
}

Step 5: Client Code

public class MediatorDemo {

    public static void main(
            String[] args) {

        ChatRoomMediator room =
                new ChatRoomMediator();

        User john =
                new ChatUser(
                        room,
                        "John");

        User mike =
                new ChatUser(
                        room,
                        "Mike");

        room.addUser(john);
        room.addUser(mike);

        john.send("Hello Everyone");
    }
}

Output

Mike received: Hello Everyone

Execution Flow

sequenceDiagram

John->>ChatRoom: send()

ChatRoom->>Mike: receive()

Mike-->>ChatRoom: delivered

Air Traffic Control Example

Without mediator:

Plane A ↔ Plane B

Plane A ↔ Plane C

Plane B ↔ Plane C

Dangerous.


With Control Tower

flowchart LR
    A[Plane A]
    B[Plane B]
    C[Plane C]
    D[Control Tower]

    A --> D
    B --> D
    C --> D

Tower coordinates all communication.


Banking Example

Loan Processing System

Components:

Loan Service

Credit Service

Fraud Service

Notification Service

Mediator coordinates workflow.


Banking Workflow

flowchart LR
    A[Loan Request]
    B[Loan Service]
    C[Credit Service]
    D[Fraud Service]
    E[Notification Service]
    F[Mediator]

    B --> F
    C --> F
    D --> F
    E --> F

    A --> B

E-Commerce Example

Order Placement:

Order Service

Inventory Service

Payment Service

Shipping Service

Notification Service

E-Commerce Flow

flowchart LR
    A[Order Service]
    B[Inventory Service]
    C[Payment Service]
    D[Shipping Service]
    E[Notification Service]
    F[Order Mediator]

    A --> F
    B --> F
    C --> F
    D --> F
    E --> F

Spring Framework Example

Spring Event System behaves like a Mediator.

Publisher:

applicationEventPublisher.publishEvent(
        new OrderCreatedEvent());

Listeners receive events.

No direct dependency.


Spring Event Flow

flowchart LR
    A[Publisher]
    B[Spring Event Bus]
    C[Listener 1]
    D[Listener 2]
    E[Listener 3]

    A --> B
    B --> C
    B --> D
    B --> E

Microservices Example

Event Driven Architecture.

Services communicate via:

Kafka

RabbitMQ

Event Bus

instead of direct calls.

Mediator concept at scale.


Kafka Flow

flowchart LR
    A[Order Service]
    B[Kafka]
    C[Inventory Service]
    D[Payment Service]
    E[Shipping Service]

    A --> B
    B --> C
    B --> D
    B --> E

Enterprise Examples

Banking

Loan Processing

Fraud Detection

Notifications

Insurance

Claim Processing

Policy Validation

Payments

Retail

Order Management

Inventory Updates

Shipping

Messaging Platforms

Slack

Teams

Chat Applications

Benefits

✅ Reduces Coupling

✅ Centralized Communication

✅ Easier Maintenance

✅ Better Scalability

✅ Cleaner Architecture

✅ Easier Testing


Limitations

❌ Mediator Can Become Large

❌ Single Point Of Failure

❌ Too Much Logic In Mediator


When To Use

Use Mediator when:

  • Many objects communicate
  • Dependencies are complex
  • Communication rules change often
  • Workflow coordination is needed

When Not To Use

Avoid when:

  • Only a few objects interact
  • Communication is simple

Mediator vs Observer

Feature Mediator Observer
Purpose Coordinate Communication Notify Subscribers
Focus Interaction Event Broadcasting
Example Chat Room Event Listener

Mediator vs Facade

Feature Mediator Facade
Goal Coordinate Objects Simplify API
Focus Communication Access
Usage Workflows Subsystems

Interview Questions

What is Mediator Pattern?

A behavioral pattern that centralizes communication between objects.


Why Use Mediator?

To reduce direct dependencies between objects.


Real World Example?

Air Traffic Control Tower.


Spring Example?

Spring Event Publishing System.


Microservices Example?

Kafka Event Bus.


Main Benefit?

Loose coupling between collaborating objects.


Key Takeaways

  • Mediator is a Behavioral Design Pattern.
  • Centralizes communication between objects.
  • Reduces tight coupling.
  • Common in Chat Systems, Workflow Engines, Spring Events, and Event-Driven Architectures.
  • Kafka and Event Bus architectures follow Mediator principles.
  • Improves maintainability and scalability.