Python Mediator Pattern
The Mediator pattern is a behavioral design pattern that reduces direct communication dependencies between objects by introducing a mediator object. Imagine an airport control tower in real life - all aircraft do not communicate directly with each other, but coordinate takeoffs, landings, and flight routes through the control tower, thus avoiding confusion and conflicts.
In software development, when complex interaction relationships exist among multiple objects, the Mediator pattern can encapsulate these interactions in the mediator, thereby reducing coupling between objects.
Core idea
The core idea of the Mediator pattern is "Do not talk directly; coordinate through the mediator". It solves the complexity of many-to-many interactions between objects, making the system easier to maintain and extend.
Why is the Mediator pattern needed
Problem Scenario
Suppose we are developing a chat room system with multiple users who need to send messages to each other. Without a mediator, the code might look like this:
Example
class
User:
def
init
(
self
,
name
)
:
self
.
name
=
name
self
.
other_users
=
[
]
needs to know all other users
def
send_message
(
self
,
message
,
to_user
)
:
(
f
"{self.name} sends to {to_user.name}: {message}"
)
to_user.
receive_message
(
message
,
self
)
def
receive_message
(
self
,
message
,
from_user
)
:
(
f
"{self.name} received a message from {from_user.name}: {message}"
)
Usage examples
user1
=
User
(
"Xiao Ming"
)
user2
=
User
(
"Xiao Hong"
)
user3
=
User
(
"Xiao Gang"
)
Every user needs to know all other users
user1.
other_users
=
[
user2
,
user3
]
user2.
other_users
=
[
user1
,
user3
]
user3.
other_users
=
[
user1
,
user2
]
user1.
send_message
(
"Hello!"
,
user2
)
Disadvantages of this design:
- High coupling between objects; every user needs to know all other users
- Adding a new user requires modifying all existing users
- Hard to maintain; interaction logic is scattered across objects
Solution
The Mediator pattern solves this problem by introducing a chat room (the mediator):
Example
class
ChatRoom:
def
init
(
self
)
:
self
.
users
=
{
}
def
register_user
(
self
,
user
)
:
self
.
users
[
user
.
name
]
=
user
user
.
chat_room
=
self
def
send_message
(
self
,
message
,
from_user
,
to_user_name
=
None
)
:
if
to_user_name:
Private chat
if
to_user_name
in
self
.
users
:
self
.
users
[
to_user_name
]
.
receive_message
(
message
,
from_user
)
else
:
Group chat
for
user_name
,
user
in
self
.
users
.
items
(
)
:
if
user_name
!=
from_user.
name
:
user
.
receive_message
(
message
,
from_user
)
class
User:
def
init
(
self
,
name
)
:
self
.
name
=
name
self
.
chat_room
=
None
def
send_message
(
self
,
message
,
to_user_name
=
None
)
:
if
self
.
chat_room
:
self
.
chat_room
.
send_message
(
message
,
self
,
to_user_name
)
def
receive_message
(
self
,
message
,
from_user
)
:
(
f
"{self.name} received a message from {from_user.name}: {message}"
)
Usage examples
chat_room
=
ChatRoom
(
)
user1
=
User
(
"Xiao Ming"
)
user2
=
User
(
"Xiao Hong"
)
user3
=
User
(
"Xiao Gang"
)
chat_room.
register_user
(
user1
)
chat_room.
register_user
(
user2
)
chat_room.
register_user
(
user3
)
user1.
send_message
(
"Hello everyone!"
)
Broadcast message
user2.
send_message
(
"Hello, Xiaoming!"
,
"Xiao Ming"
)
Private chat
Implementation of the Mediator pattern
Basic structure
Let us understand the implementation of the Mediator pattern through a more complete example:
Example
from
abc
import
ABC
,
abstractmethod
from
typing
import
List
Abstract mediator
class
Mediator
(
ABC
)
:
@
abstractmethod
def
notify
(
self
,
sender:
object
,
event:
str
,
data:
dict
=
None
)
:
pass
Concrete mediator - Chat room
class
ChatRoomMediator
(
Mediator
)
:
def
init
(
self
)
:
self
.
users
: List
[
User
]
=
[
]
def
add_user
(
self
,
user
)
:
self
.
users
.
append
(
user
)
user
.
set_mediator
(
self
)
def
notify
(
self
,
sender
,
event
,
data
=
None
)
:
if
event
==
"send_message"
:
message
=
data.
get
(
"message"
)
target_user
=
data.
get
(
"target_user"
)
if
target_user:
Private chat
for
user
in
self
.
users
:
if
user
.
name
==
target_user:
user
.
receive_message
(
message
,
sender.
name
)
else
:
Group chat
for
user
in
self
.
users
:
if
user
!=
sender:
user
.
receive_message
(
message
,
sender.
name
)
elif
event
==
"user_joined"
:
message
=
f
"System: {sender.name} joined the chat room"
for
user
in
self
.
users
:
if
user
!=
sender:
user
.
receive_message
(
message
,
"System"
)
Base component class
class
BaseComponent:
def
init
(
self
)
:
self
._mediator
=
None
def
set_mediator
(
self
,
mediator: Mediator
)
:
self
._mediator
=
mediator
Concrete component - User
class
User
(
BaseComponent
)
:
def
init
(
self
,
name
)
:
super
(
)
.
init
(
)
self
.
name
=
name
def
send_message
(
self
,
message
,
target_user
=
None
)
:
(
f
"{self.name} sends message: {message}"
)
self
._mediator.
notify
(
self
,
"send_message"
,
{
"message"
: message
,
"target_user"
: target_user
}
)
def
join_chat
(
self
)
:
self
._mediator.
notify
(
self
,
"user_joined"
)
def
receive_message
(
self
,
message
,
from_name
)
:
(
f
"{self.name} received a message from {from_name}: {message}"
)
Usage examples
def
main
(
)
:
Create the mediator
chat_room
=
ChatRoomMediator
(
)
Create users
alice
=
User
(
"Alice"
)
bob
=
User
(
"Bob"
)
charlie
=
User
(
"Charlie"
)
Register users with the chat room
chat_room.
add_user
(
alice
)
chat_room.
add_user
(
bob
)
chat_room.
add_user
(
charlie
)
(
"=== Chat room demo ==="
)
Alice joins the chat room
alice.
join_chat
(
)
Send the message
alice.
send_message
(
"Hello everyone, I am Alice!"
)
bob.
send_message
(
"Welcome, Alice!"
)
charlie.
send_message
(
"Hello, Alice!"
,
"Alice"
)
Private chat
Bob sends a group message
bob.
send_message
(
"Is anyone online?"
)
if
name
==
"main"
:
main
(
)
Run Results
=== 聊天室演示 ===
系统: Alice 加入了聊天室
Alice 发送消息: 大家好,我是 Alice!
Bob 收到来自 Alice 的消息: 大家好,我是 Alice!
Charlie 收到来自 Alice 的消息: 大家好,我是 Alice!
Bob 发送消息: 欢迎 Alice!
Alice 收到来自 Bob 的消息: 欢迎 Alice!
Charlie 收到来自 Bob 的消息: 欢迎 Alice!
Charlie 发送消息: Alice 你好!
Alice 收到来自 Charlie 的消息: Alice 你好!
Bob 发送消息: 有人在线吗?
Alice 收到来自 Bob 的消息: 有人在线吗?
Charlie 收到来自 Bob 的消息: 有人在线吗?
UML structure of the Mediator pattern

Structure description
- Mediator (mediator interface): Defines the communication interface
- ConcreteMediator (concrete mediator): Implements coordination logic and knows all components
- BaseComponent (base component): Contains a reference to the mediator
- ConcreteComponent (concrete component): Implements specific business logic
Practical Application Scenarios
Scenario 1: GUI application
In a graphical user interface, various controls (buttons, text boxes, check boxes, etc.) coordinate their interactions through the mediator:
Example
class
DialogMediator:
def
init
(
self
)
:
self
.
login_button
=
None
self
.
username_input
=
None
self
.
password_input
=
None
self
.
remember_checkbox
=
None
def
notify
(
self
,
sender
,
event
)
:
if
event
==
"username_changed"
or
event
==
"password_changed"
:
When a username or password is entered, check whether the login button can be enabled
username
=
self
.
username_input
.
get_text
(
)
password
=
self
.
password_input
.
get_text
(
)
self
.
login_button
.
set_enabled
(
bool
(
username
and
password
)
)
elif
event
==
"login_clicked"
:
Handle login logic
username
=
self
.
username_input
.
get_text
(
)
password
=
self
.
password_input
.
get_text
(
)
remember
=
self
.
remember_checkbox
.
is_checked
(
)
(
f
"Login: {username}, Remember me: {remember}"
)
elif
event
==
"remember_changed"
:
(
"Remember me option has changed"
)
class
UIComponent:
def
init
(
self
,
mediator
=
None
)
:
self
.
mediator
=
mediator
def
set_mediator
(
self
,
mediator
)
:
self
.
mediator
=
mediator
class
Button
(
UIComponent
)
:
def
click
(
self
)
:
if
self
.
mediator
:
self
.
mediator
.
notify
(
self
,
"login_clicked"
)
def
set_enabled
(
self
,
enabled
)
:
(
f
"Button {'enabled' if enabled else 'disabled'}"
)
class
TextInput
(
UIComponent
)
:
def
init
(
self
,
mediator
=
None
)
:
super
(
)
.
init
(
mediator
)
self
._text
=
""
def
set_text
(
self
,
text
)
:
self
._text
=
text
if
self
.
mediator
:
self
.
mediator
.
notify
(
self
,
"username_changed"
)
def
get_text
(
self
)
:
return
self
._text
Usage examples
dialog
=
DialogMediator
(
)
login_btn
=
Button
(
)
username_input
=
TextInput
(
)
password_input
=
TextInput
(
)
dialog.
login_button
=
login_btn
dialog.
username_input
=
username_input
dialog.
password_input
=
password_input
login_btn.
set_mediator
(
dialog
)
username_input.
set_mediator
(
dialog
)
password_input.
set_mediator
(
dialog
)
Simulate user input
username_input.
set_text
(
"user123"
)
password_input.
set_text
(
"pass123"
)
login_btn.
click
(
)
Scenario 2: E-commerce order system
In an e-commerce system, order processing involves multiple subsystems such as inventory, payment, and logistics:
Example
class
OrderMediator:
def
init
(
self
)
:
self
.
inventory_system
=
None
self
.
payment_system
=
None
self
.
shipping_system
=
None
self
.
notification_system
=
None
def
place_order
(
self
,
order_data
)
:
(
"=== Start processing order ==="
)
Check inventory
if
not
self
.
inventory_system
.
check_stock
(
order_data
)
:
return
"Insufficient inventory"
Process payment
payment_result
=
self
.
payment_system
.
process_payment
(
order_data
)
if
not
payment_result:
return
"Payment failed"
Update inventory
self
.
inventory_system
.
update_stock
(
order_data
)
Arrange shipment
shipping_info
=
self
.
shipping_system
.
schedule_delivery
(
order_data
)
Send notification
self
.
notification_system
.
send_confirmation
(
order_data
,
shipping_info
)
return
"Order processed successfully"
class
InventorySystem:
def
check_stock
(
self
,
order_data
)
:
(
"Checking inventory..."
)
return
True
def
update_stock
(
self
,
order_data
)
:
(
"Updating inventory..."
)
class
PaymentSystem:
def
process_payment
(
self
,
order_data
)
:
(
"Processing payment..."
)
return
True
class
ShippingSystem:
def
schedule_delivery
(
self
,
order_data
)
:
(
"Arranging shipment..."
)
return
"Tracking number: SF123456789"
class
NotificationSystem:
def
send_confirmation
(
self
,
order_data
,
shipping_info
)
:
(
f
"Send confirmation email, shipping info: {shipping_info}"
)
Usage examples
mediator
=
OrderMediator
(
)
mediator.
inventory_system
=
InventorySystem
(
)
mediator.
payment_system
=
PaymentSystem
(
)
mediator.
shipping_system
=
ShippingSystem
(
)
mediator.
notification_system
=
NotificationSystem
(
)
order_data
=
{
"product_id"
:
"123"
,
"quantity"
:
2
,
"user_id"
:
"user001"
}
result
=
mediator.
place_order
(
order_data
)
(
f
"Order result: {result}"
)
Advantages and disadvantages of the Mediator pattern
Advantages
| Advantages | Description |
|---|---|
| Single responsibility principle | Centralizes interaction logic between components in the mediator |
| Open-Closed Principle | New mediators can be introduced without modifying existing components |
| Reduced coupling | Reduces direct dependencies between components |
| Easy to reuse | Individual components can be reused more easily in different contexts |
Disadvantages
| Disadvantages | Description |
|---|---|
| The mediator may become complex | As interaction logic grows, the mediator may become a god object |
| Performance overhead | All communication passes through the mediator, which may introduce performance bottlenecks |
| Difficult to debug | Interaction logic is concentrated in the mediator, so debugging may require tracking multiple components |
Best practices and notes
1. Divide mediator responsibilities reasonably
Do not let one mediator handle everything; you can divide it into multiple mediators by business domain:
Example
class
UserRegistrationMediator:
def
notify
(
self
,
sender
,
event
,
data
)
:
if
event
==
"user_registered"
:
Handle logic related to post-registration
self
.
send_welcome_email
(
data
)
self
.
create_user_profile
(
data
)
self
.
add_to_mailing_list
(
data
)
class
OrderProcessingMediator:
def
notify
(
self
,
sender
,
event
,
data
)
:
if
event
==
"order_created"
:
Handle logic related to post-order-creation
self
.
validate_order
(
data
)
self
.
process_payment
(
data
)
self
.
update_inventory
(
data
)
2. Avoid making the mediator overly complex
If the mediator becomes too complex, consider using other patterns or refactoring:
Example
Bad practice - the mediator contains too much logic
class
ComplexMediator:
def
handle_event
(
self
,
event_type
,
data
)
:
if
event_type
==
"user_action"
:
A large amount of complex business logic...
pass
elif
event_type
==
"system_event"
:
More complex logic...
pass
Good practice - use the Strategy pattern to break down complex logic
class
EventHandler:
def
handle
(
self
,
data
)
:
pass
class
UserActionHandler
(
EventHandler
)
:
def
handle
(
self
,
data
)
:
Specifically handle user actions
pass
class
SimpleMediator:
def
init
(
self
)
:
self
.
handlers
=
{
}
def
register_handler
(
self
,
event_type
,
handler
)
:
self
.
handlers
[
event_type
]
=
handler
def
notify
(
self
,
event_type
,
data
)
:
if
event_type
in
self
.
handlers
:
self
.
handlers
[
event_type
]
.
handle
(
data
)
3. Consider combining with the Observer pattern
The Mediator pattern is often used in combination with the Observer pattern:
Example
from
abc
import
ABC
,
abstractmethod
from
typing
import
List
class
Observer
(
ABC
)
:
@
abstractmethod
def
update
(
self
,
event
,
data
)
:
pass
class
Observable:
def
init
(
self
)
:
self
._observers: List
[
Observer
]
=
[
]
def
add_observer
(
self
,
observer
)
:
self
._observers.
append
(
observer
)
def
notify_observers
(
self
,
event
,
data
=
None
)
:
for
observer
in
self
._observers:
observer.
update
(
event
,
data
)
class
ChatMediator
(
Observable
)
:
def
send_message
(
self
,
message
,
sender
,
receiver
=
None
)
:
Mediator logic
event_data
=
{
"message"
: message
,
"sender"
: sender
,
"receiver"
: receiver
}
self
.
notify_observers
(
"message_sent"
,
event_data
)
class
LoggingObserver
(
Observer
)
:
def
update
(
self
,
event
,
data
)
:
if
event
==
"message_sent"
:
(
f
"Log: {data['sender']} sent a message"
)
Exercises
Exercise 1: Improve the chat room system
Extend the previous chat room system by adding the following features:
- Support creating multiple chat rooms
- Support users switching between different chat rooms
- Add administrator features (kicking, muting, etc.)
Exercise 2: Design a flight booking system
Use the Mediator pattern to design a flight booking system with the following components:
- Flight query
- Seat selection
- Payment processing
- Ticket generation
- Notification sending
Require each component to coordinate work through the mediator.
Exercise 3: Smart home control system
Design a smart home control system that includes:
- Lighting control
- Temperature adjustment
- Security monitoring
- Scene modes
Implement intelligent linkage between devices through the mediator.
Summary
The Mediator pattern is a powerful tool for handling complex object interactions. It simplifies system architecture by introducing a coordination center, and is especially suitable for the following scenarios:
- Complex network-like interaction relationships exist between objects
- Interaction logic among multiple objects needs to be centrally controlled
- You want to reduce coupling between objects
Remember, the Mediator pattern is not a cure-all; when interaction logic is simple, direct communication between objects may be more appropriate. But when dealing with complex systems, reasonable use of the Mediator pattern can significantly improve code maintainability and extensibility.
Key points:
- The mediator encapsulates interaction logic between objects
- Components communicate only with the mediator and do not directly depend on each other
- The mediator may become complex and needs reasonable design
- Combining with other patterns (such as Observer) can yield better results