Skip to main content

🧩 Mediator Pattern

✅ Intent

  • Enable components to communicate through a mediator instead of referencing each other directly
  • Centralize the relationships to make interactions easier to manage

✅ Motivation

  • Useful when you want a central coordinating class to manage multiple components
  • Allows components to remain independent while still interacting

✅ When to Use

  • When you need to manage relationships between multiple components (e.g., UI, forms, or services)
  • When you want to make control flow and coordination more visible and manageable

✅ Code Example

// Mediator インターフェース
interface Mediator {
notify(sender: string, data: any): void;
}

// コンポーネント(UserService)
class UserService {
constructor(private mediator: Mediator) {}

registerUser(email: string): void {
console.log(`ユーザー登録: ${email}`);
this.mediator.notify("UserService", email);
}
}

// コンポーネント(Mailer)
class Mailer {
sendWelcome(email: string): void {
console.log(`送信しました: ${email} → ようこそ!`);
}
}

// Mediator 実装
class AppMediator implements Mediator {
constructor(private mailer: Mailer) {}

notify(sender: string, data: any): void {
if (sender === "UserService") {
this.mailer.sendWelcome(data);
}
}
}

// 利用例
const mailer = new Mailer();
const mediator = new AppMediator(mailer);
const userService = new UserService(mediator);

userService.registerUser("user@example.com");

✅ Explanation

This code applies the Mediator pattern to remove direct dependencies between components (UserService and Mailer)
and enables communication through a centralized Mediator.
The Mediator pattern delegates communication between objects to a central mediator,
thus reducing coupling between components.

1. Overview of the Mediator Pattern

  • Mediator: Interface for mediating communication between components

    • Represented by Mediator
  • ConcreteMediator: Implements the Mediator interface and contains the logic to coordinate components

    • Represented by AppMediator
  • Colleague: Components that interact through the mediator

    • Represented by UserService and Mailer

2. Key Classes and Their Roles

  • Mediator

    • Interface for coordinating component communication
    • Declares notify(sender: string, data: any): void
  • AppMediator

    • Concrete implementation of Mediator
    • Handles communication logic in notify, routing messages from UserService to Mailer
  • UserService

    • A colleague component
    • Performs user registration and notifies the mediator of events
  • Mailer

    • Another colleague component
    • Receives notification from the mediator and sends emails

3. UML Class Diagram

4. Benefits of the Mediator Pattern

  • Loose Coupling: Eliminates direct dependencies between components, reducing complexity
  • Extensibility: New components can be integrated by simply updating the Mediator
  • Centralized Coordination: Interaction logic is centralized, improving readability and maintainability

This design is highly effective when multiple components need to communicate,
while preserving modularity and scalability of the codebase.