Skip to main content

What It Is

The Mediator pattern moves communication between many objects into one central place. Instead of everyone knowing everyone (and n objects meaning n·n connections), each object knows only the mediator, and the mediator routes.


Example: A Chat Room

Users never hold references to each other. A user sends to the room; the room delivers to everyone else. Joining and leaving touches one list, in one place.

Everyone talks through the room

Ana knows only the room; the room knows everyone. Delivery rules — skip the sender, later maybe mute or private messages — live in exactly one method.

Prompt
Write a Go program with the Mediator pattern: a ChatRoom that users join and that broadcasts a sender's message to every other user, and User types that only know the room. Have three users join and one of them send a message, printing what each other user receives.
Output: Go program and captured output

When to Use It

Use it when many objects coordinate and the web of who-tells-whom is getting dense — chat systems, form fields that enable and disable each other, air-traffic-style coordination.

Skip it for two or three objects with simple links. A mediator between two parties is a middleman nobody hired.


Pros and Cons

Pros: participants stay simple and reusable; the coordination rules are readable in one place; adding a participant touches nothing else.

Cons: the mediator grows — it can quietly become the huge type that knows everything; all traffic through one point can hide performance costs.