intermediateMediator Pattern

In a chat-room mediator, why does the mediator need to distinguish between a user sending and other users receiving a message?

The mediator's broadcast method is called once by the sender, but it needs to deliver the message to every other participant without echoing it back to the sender itself. If the mediator called the exact same method on every participant, including the sender, the sender would receive its own message back — a subtle bug that is easy to miss without deliberately testing for it. By checking that a given participant is not the original sender before delivering to them, and by exposing a distinct receive method that the mediator calls, as opposed to the send method a participant calls to initiate a broadcast, the mediator makes the sender-and-receiver distinction explicit rather than accidental.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Most behavioral patterns favor composition over inheritance. Why does Template Method deliberately do the opposite?← Back to all Low-Level Design & Design Patterns questions