Mediator Pattern in Python
Route messages between objects through one coordinator, like users in a chat room, so they never reference each other.
- Colleagues and a mediator
- Sending messages through the mediator
- Reducing many-to-many dependencies
ChatRoommediatorAdasends "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.
ChatRoom💬 The chat room in the middleUser("Ada", room)📱 A member who only knows the roomself.room.send(...)📤 Send to the room, not to another memberrecipient.receive(...)📥 The room delivers it to the right memberThe Mediator pattern routes communication between objects through one coordinating object, so the objects do not refer to each other directly.
Key points
- Each
Userknows onlyroom, the sharedChatRoom. ada.send(lin, "Hello")asks the room to deliver the message.ChatRoom.sendcallsrecipient.receive(...)on the right user.- Adding more users does not change the
Userclass.
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
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")
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.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Users calling each other directly
Every user then needs references to every other user.
def send(self, recipient, message):
recipient.receive(self.name, message)
def send(self, recipient, message):
self.room.send(self, recipient, message)
Finished this lesson?
Mark it as learned to track your progress.