cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 174 of 218
Ideas this builds on
Low-level design174 / 218

Mediator: centralize collaboration

Picture

A chat participant sends through a room. The room decides which other participants receive the message.

Predict

Must each participant hold references to every other participant?

Walk through the small example

No. The mediator owns how the peers collaborate.

Mental model and complete Python solution

Room maps member names to receiver callbacks. It validates the sender and broadcasts to everyone else. Unlike a facade’s simpler external entry point, the mediator defines peer interaction rules. The sample is synchronous, local, and not durable; receiver failures can produce partial delivery.

What the function promises: Member names are unique; unknown senders raise. No real messages leave the process.

class Room:
    def __init__(self):
        self.receivers = {}
    def join(self, name, receiver):
        if name in self.receivers:
            raise ValueError("Duplicate member")
        self.receivers[name] = receiver
    def send(self, sender, text):
        if sender not in self.receivers:
            raise ValueError("Unknown sender")
        for name, receiver in tuple(self.receivers.items()):
            if name != sender:
                receiver((sender, text))

room, ana, bo = Room(), [], []
room.join("Ana", ana.append)
room.join("Bo", bo.append)
room.send("Ana", "hello")
assert ana == [] and bo == [("Ana", "hello")]

Work and storage: O(recipients) notification work plus listener costs.

When to recognize it: A mediator reduces tangled peer references. It should not absorb every unrelated feature just because messages pass through it.

Check before applying it: A mediator does not automatically provide reliable distributed delivery.

Change one rule

Participants reconnect across servers. What is missing?

See what changes

Durable membership and message history, authorization, IDs, deduplication, and routing ownership. The high-level chat lesson adds those boundaries.

Connect

Facade: offer one clear entry point: A facade serves callers; a mediator defines interactions among peers.

Design an elevator controller: An elevator dispatcher coordinates cars and requests through one policy boundary.