Behavioral Design Pattern

Mediator Design Pattern in Java

Centralize communication between objects to reduce direct dependencies.

In this lesson: Mediator moves communication logic into a central object. Colleagues notify the mediator instead of calling one another directly.

Overview

interface ChatMediator { void send(String message, User sender); } abstract class User { protected final ChatMediator mediator; User(ChatMediator mediator) { this.mediator = mediator; } }

Implementation

final class ChatRoom implements ChatMediator { private final List<User> users = new ArrayList<>(); void add(User user) { users.add(user); } public void send(String message, User sender) { users.forEach(user -> { if (user != sender) System.out.println(message); }); } }
Use Mediator for chat rooms, UI components, workflow coordination, and systems where peer-to-peer dependencies have become difficult to manage.

When should you use it?

Use Mediator when peer objects have too many direct dependencies and their communication needs a clear coordination point.

Next step: Continue with the Design Patterns course and explore the next pattern.