Python Design Patterns

Mediator Pattern in Python

Route messages between objects through one coordinator, like users in a chat room, so they never reference each other.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • Colleagues and a mediator
  • Sending messages through the mediator
  • Reducing many-to-many dependencies
ChatRoom
  • ChatRoommediator
    • Adasends "Hello"
    • LinLin from Ada: Hello

Talk through the hub

The problem

When many objects talk to each other directly, such as chat users, form fields or aircraft, every object must know about every other one. The connections multiply and changing one object affects many others.

Think of it like this

A mediator is like a group chat app. Ada never sends a message to Lin's phone directly; she sends it to the chat room, and the room delivers it to Lin with Ada's name on it. A new member only needs to join the room, not to know everyone else.

From a group chat to code
ChatRoom💬 The chat room in the middle
User("Ada", room)📱 A member who only knows the room
self.room.send(...)📤 Send to the room, not to another member
recipient.receive(...)📥 The room delivers it to the right member
Objects communicate through one coordinator.
In one sentence

The Mediator pattern routes communication between objects through one coordinating object, so the objects do not refer to each other directly.

Key points

  1. Each User knows only room, the shared ChatRoom.
  2. ada.send(lin, "Hello") asks the room to deliver the message.
  3. ChatRoom.send calls recipient.receive(...) on the right user.
  4. Adding more users does not change the User class.

When should you use it?

  • Chat rooms and message hubs.
  • Forms where fields depend on each other.
  • Coordinating several components that would otherwise reference each other.

When not to use it

  • Two objects that talk only to each other: a direct call is simpler.
  • The mediator grows into a class that knows everything and does everything.

Example code

Run code →
main.py
class ChatRoom:
    # Mediator: route colleague communication through a coordinator.
    def send(self, sender, recipient, message):
        # Coordinate communication between colleague objects.
        recipient.receive(sender.name, message)


class User:
    def __init__(self, name, room): self.name, self.room = name, room
    def send(self, recipient, message): self.room.send(self, recipient, message)
    def receive(self, sender, message): print(f"{self.name} from {sender}: {message}")


room = ChatRoom()
ada, lin = User("Ada", room), User("Lin", room)
ada.send(lin, "Hello")
Get a Run button for this example

Result of running the code

Lin from Ada: Hello

Code explanation

Every User is created with a reference to the same ChatRoom. When Ada sends a message, her send method passes it to the room instead of calling Lin directly. ChatRoom.send then calls receive on the recipient, so Lin prints the message without the User class knowing how delivery works.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Replace the sample domain while preserving the pattern structure.
  2. Compare the pattern with a simpler implementation and explain the tradeoff.
  3. Write tests for each participant before adding another implementation.
Run in the Python compiler →

Common mistakes

Users calling each other directly

Every user then needs references to every other user.

Problem
def send(self, recipient, message):
    recipient.receive(self.name, message)
Fix
def send(self, recipient, message):
    self.room.send(self, recipient, message)

Finished this lesson?

Mark it as learned to track your progress.