Knowledge Guide
HomeOO & Low-Level DesignBehavioral

Mediator Pattern

The Mediator pattern serves as a centralized communication medium for various objects in a system. The pattern seeks to reduce coupling, increase system scalability, and facilitate easier understanding and maintenance by minimizing direct communication between objects or classes.

For instance, within an office setting, communication between different departments (such as HR, Finance, and Technical) is often needed. Direct communication between departments for every minor query or update can result in an unorganized and inefficient workflow. Suppose the Technical department requires data for a new project from HR and Finance. In that case, handling this communication can get complicated and time-consuming. The image below illustrates the scenario:

Mediator Pattern - Office Communication Problem
Mediator Pattern - Office Communication Problem

Implementing the Mediator pattern in this scenario involves setting up a central communication office or a mediator. This office serves as the primary point of contact for interdepartmental communication. This office receives all requests and information and forwards it to the relevant department.

Mediator Pattern - Office Communication Solution
Mediator Pattern - Office Communication Solution

Real-World Example

Consider the role of event organizers in the planning of a large event such as a music festival. Many parties are involved in the event, including vendors, venue management, security, and performers.

Direct Communication Issue: The process would soon become unmanageable if all parties communicated with each other directly for every issue. It would be chaotic, for example, if each vendor had to communicate directly with performers and security to meet their needs.

Solution for Mediator: An event planner serves as a mediator. They arrange for all correspondence and arrangements between the various stakeholders. The event organizer receives updates and needs from vendors, security, performers, and venue management and forwards them to the relevant parties. This streamlines and expedites the process by centralizing communication.

For better understanding, this is illustrated in the image below:

Mediator Pattern -Real-World Example
Mediator Pattern -Real-World Example

Structure of Mediator Pattern

Through the mediator, components communicate with one another. They communicate with the mediator, who forwards their messages to the relevant component. The mediator centralizes complex communication and control logic between related objects.

Mediator Pattern - Class Diagram
Mediator Pattern - Class Diagram

Implementation of Mediator Pattern

Recap the office example discussed in the previous section. Departments such as HR, Finance, and Technical need to communicate with one another. Direct communication between departments can be chaotic and ineffective.

Let's implement its solution through mediator pattern.

// Mediator Interface interface DepartmentMediator { communicate(message: String, department: Department) } // Concrete Mediator class OfficeMediator implements DepartmentMediator { HRDepartment hr FinanceDepartment finance TechnicalDepartment technical setHRDepartment(department: HRDepartment) { hr = department } setFinanceDepartment(department: FinanceDepartment) { finance = department } setTechnicalDepartment(department: TechnicalDepartment) { technical = department } communicate(message: String, department: Department) { if (department == hr) { finance.receiveMessage(message) technical.receiveMessage(message) } else if (department == finance) { // similar logic } else if (department == technical) { // similar logic } } } // Department Interface interface Department { setMediator(mediator: DepartmentMediator) sendMessage(message: String) receiveMessage(message: String) } // Concrete Departments class HRDepartment implements Department { DepartmentMediator mediator setMediator(mediator: DepartmentMediator) { this.mediator = mediator } sendMessage(message: String) { mediator.communicate(message, this) } receiveMessage(message: String) { // Handle received message } } // Similar classes for FinanceDepartment and TechnicalDepartment // Client Code main() { mediator = new OfficeMediator() hr = new HRDepartment() finance = new FinanceDepartment() technical = new TechnicalDepartment() mediator.setHRDepartment(hr) mediator.setFinanceDepartment(finance) mediator.setTechnicalDepartment(technical) hr.setMediator(mediator) finance.setMediator(mediator) technical.setMediator(mediator) hr.sendMessage("HR update for all departments") }

Implementation

java
interface Mediator {
  void sendMessage(String message, Department department);
}

interface Department {
  void setMediator(Mediator mediator);
  void sendMessage(String message);
  void receiveMessage(String message);
}

class HRDepartment implements Department {

  private Mediator mediator;

  public void setMediator(Mediator mediator) {
    this.mediator = mediator;
  }

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

  public void receiveMessage(String message) {
    System.out.println("HR Department received: " + message);
  }
}

class FinanceDepartment implements Department {

  private Mediator mediator;

  public void setMediator(Mediator mediator) {
    this.mediator = mediator;
  }

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

  public void receiveMessage(String message) {
    System.out.println("Finance Department received: " + message);
  }
}

// Similar class for FinanceDepartment

class OfficeMediator implements Mediator {

  private HRDepartment hr;
  private FinanceDepartment finance;

  public void setHRDepartment(HRDepartment hr) {
    this.hr = hr;
  }

  public void setFinanceDepartment(FinanceDepartment finance) {
    this.finance = finance;
  }

  public void sendMessage(String message, Department department) {
    if (department.equals(hr) && finance != null) {
      finance.receiveMessage(message);
    } else if (department.equals(finance) && hr != null) {
      hr.receiveMessage(message);
    }
  }
}

public class Solution {

  public static void main(String[] args) {
    OfficeMediator mediator = new OfficeMediator();
    HRDepartment hr = new HRDepartment();
    FinanceDepartment finance = new FinanceDepartment();

    mediator.setHRDepartment(hr);
    mediator.setFinanceDepartment(finance);

    hr.setMediator(mediator);
    finance.setMediator(mediator);

    hr.sendMessage("HR update for all departments");
  }
}

Application of Mediator Pattern

The Mediator pattern is widely used to manage complex communications and interactions in a variety of fields. Primary applications consist of:

Reduced component coupling and centralized control simplify interactions, increase system manageability, and enable scalability for every application.

Pros and Cons

ProsCons
Reduces Dependencies
Reduces the dependencies between communicating objects or classes.
Complex Mediator
The mediator can become overly complex as it centralizes control.
Centralizes Control
Simplifies communication by centralizing it in one object or class.
Single Point of Failure
The mediator becomes a critical part of the system, and its failure can impact the entire system.
Simplifies Maintenance
It is easier to maintain and modify communication logic in one place.
Performance Bottleneck
All communication through the mediator may lead to performance issues in large systems.
Promotes Loose Coupling
Helps in building systems where components are loosely coupled.
Debugging Challenges
Debugging can be difficult if the mediator's logic is too complex.

The Mediator pattern is primarily used to manage complex system communications. It performs best when it is important to minimize direct coupling between components. By centralized communication, the system becomes easier to understand and maintain. But, it's crucial to be aware of the complexity it may bring along with the possibility of single points of failure and performance bottlenecks. When used properly, the Mediator pattern can result in a code architecture that is scalable, clean, and well-organized.

🤖 Don't fully get this? Learn it with Claude

Stuck on Mediator Pattern? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Mediator Pattern** (OO & Low-Level Design) and want to truly understand it. Explain Mediator Pattern from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Mediator Pattern** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Mediator Pattern** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Mediator Pattern** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes