Skip to main content

Sign in to CleanKata

Track your progress, earn XP, and unlock every lesson.

By signing in you agree to our Terms of Use and Privacy Policy.

Design Patterns80 XP8 min

Mediator: Centralized Communication

Restrict direct communication between objects — force them to collaborate through a mediator to reduce chaotic many-to-many dependencies.

Why this matters

Think of an airport. Planes don't talk to each other directly — they all communicate through the control tower. Without the tower, every plane would need to know about every other plane. With it, each plane only needs one connection. Mediator applies the same hub-and-spoke model to your objects: instead of N objects each holding references to N-1 others (O(N²) connections), every object holds one reference to the mediator (O(N) connections).

Real-world appearances

UI frameworks use Mediator constantly. A form with many fields that enable/disable each other shouldn't have fields wired to each other — a form controller (mediator) handles it. Redux and Vuex are Mediators for application state. Event buses (Node.js EventEmitter, browser CustomEvent) are mediators. Chat servers, message brokers (RabbitMQ, Kafka) — all variations of the same idea.

💡Key takeaway

When too many objects communicate directly with too many others, Mediator extracts that complexity into one place — a hub that everyone talks to, reducing O(N²) connections to O(N).

🔧 Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it — it helps us fix it fast.

Hint: When too many objects communicate directly with too many others, Mediator extracts that complexity into one place — a hub that everyone talks to.

✗ Your version

Mediator: Centralized Communication — CleanKata — CleanKata