The Mediator Delivers

Step 1: The Mediator Delivers

In the Mediator pattern, objects do not talk to each other directly. They talk through a central mediator that coordinates the interaction, which keeps the individual objects decoupled and simple.

Here the mediator is a ChatRoom. A User has a name, an inbox (a list of messages it has received), and a room reference. The ChatRoom keeps a list of registered users.

register(user) has already been written for you: it adds the user to the room and sets user.room to the room.

Your job is to implement broadcast(sender, message). It must deliver the message to every registered user EXCEPT the sender, by appending the message to each recipient's inbox. The sender never receives its own message.

Notice that users never touch each other: the room is the only thing that knows the full membership.

Hints
Starter code
class User:
    def __init__(self, name):
        self.name = name
        self.inbox = []
        self.room = None


class ChatRoom:
    def __init__(self):
        self.users = []

    def register(self, user):
        self.users.append(user)
        user.room = self

    def broadcast(self, sender, message):
        """Deliver message to every registered user except the sender."""
        # TODO: loop over self.users and append message to each inbox,
        # skipping the sender
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.