
Application of the Mediator Pattern in C Language (Including Linux Kernel Examples)
1. Definition and Core Value of the Mediator Pattern
The Mediator Pattern is a behavioral design pattern that focuses onintroducing a mediator object to encapsulate the interaction logic between multiple objects (colleagues), allowing colleagues to interact indirectly through the mediator instead of directly communicating with each other. The mediator is responsible for coordinating the relationships between colleagues, reducing the coupling between objects.
Significance: When there are complex many-to-many interactions between multiple objects, a tightly coupled “mesh structure” forms among the objects, where modifying one object may require adjustments to multiple related objects, leading to high maintenance costs. The Mediator Pattern transforms many-to-many relationships into one-to-many relationships, managing interaction logic centrally through the mediator, thus maintaining loose coupling between objects.
Problems Solved:
- • High coupling caused by direct communication between multiple objects (e.g., “Module A calls Module B, Module B calls Module C, and Module C calls Module A”);
- • Interaction logic scattered across multiple objects, making it difficult to maintain and extend uniformly;
- • Adding or removing objects requires modifying the interaction code of multiple related objects.
2. Core Idea of Implementing the Mediator Pattern in C Language
The C language implements the Mediator Pattern throughstructs to define the mediator and colleagues + function pointers to implement interaction interfaces:
- 1. Define the mediator interface (Mediator): a struct containing function pointers for registering colleagues and forwarding messages, responsible for coordinating all colleagues’ interactions;
- 2. Define the colleague interface (Colleague): a struct containing a pointer to the mediator and function pointers for sending messages, allowing colleagues to communicate through the mediator instead of directly with other colleagues;
- 3. Implement a concrete mediator: maintain a list of colleagues and implement message forwarding logic (e.g., dispatching to target colleagues based on message type);
- 4. Implement concrete colleagues: send messages through the mediator, receive messages forwarded by the mediator, and process them.

3. 5 Examples
Example 1: Basic Mediator (Chat Room Message Forwarding)
Simulating a chat room scenario, users (colleagues) send messages through the chat room (mediator), which forwards the messages to all other users.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Forward declaration
typedef struct Mediator Mediator;
typedef struct Colleague Colleague;
// Colleague interface
typedef struct Colleague {
char name[32];
Mediator* mediator; // Pointer to mediator
void (*send)(struct Colleague* self, const char* msg); // Send message
void (*receive)(struct Colleague* self, const char* from, const char* msg); // Receive message
} Colleague;
// Mediator interface
typedef struct Mediator {
Colleague* colleagues[10]; // List of colleagues
int count; // Number of colleagues
void (*register_colleague)(struct Mediator* self, Colleague* col); // Register colleague
void (*relay)(struct Mediator* self, Colleague* sender, const char* msg); // Forward message
} Mediator;
// Concrete colleague: User
static void user_send(Colleague* self, const char* msg) {
printf("[%s] Sending: %s\n", self->name, msg);
self->mediator->relay(self->mediator, self, msg); // Forward through mediator
}
static void user_receive(Colleague* self, const char* from, const char* msg) {
printf("[%s] Received from %s: %s\n", self->name, from, msg);
}
Colleague* create_user(const char* name, Mediator* mediator) {
Colleague* user = malloc(sizeof(Colleague));
strncpy(user->name, name, sizeof(user->name)-1);
user->mediator = mediator;
user->send = user_send;
user->receive = user_receive;
return user;
}
// Concrete mediator: Chat Room
static void chatroom_register(Mediator* self, Colleague* col) {
if (self->count < 10) {
self->colleagues[self->count++] = col;
printf("User %s joined chatroom\n", col->name);
}
}
static void chatroom_relay(Mediator* self, Colleague* sender, const char* msg) {
// Forward message to all other colleagues
for (int i = 0; i < self->count; i++) {
Colleague* receiver = self->colleagues[i];
if (receiver != sender) { // Do not send to self
receiver->receive(receiver, sender->name, msg);
}
}
}
Mediator* create_chatroom() {
Mediator* room = malloc(sizeof(Mediator));
room->count = 0;
room->register_colleague = chatroom_register;
room->relay = chatroom_relay;
return room;
}
// Free resources
void free_colleagues(Colleague** cols, int count) {
for (int i = 0; i < count; i++) free(cols[i]);
}
void free_mediator(Mediator* m) {
free(m);
}
// Client usage
int main() {
Mediator* chatroom = create_chatroom();
// Create users and register to chatroom
Colleague* alice = create_user("Alice", chatroom);
Colleague* bob = create_user("Bob", chatroom);
Colleague* charlie = create_user("Charlie", chatroom);
chatroom->register_colleague(chatroom, alice);
chatroom->register_colleague(chatroom, bob);
chatroom->register_colleague(chatroom, charlie);
// Send messages (forwarded through mediator)
printf("\n");
alice->send(alice, "Hello everyone!");
printf("\n");
bob->send(bob, "Hi Alice!");
// Free resources
Colleague* cols[] = {alice, bob, charlie};
free_colleagues(cols, 3);
free_mediator(chatroom);
return 0;
}
The output of the above code is as follows
User Alice joined chatroom
User Bob joined chatroom
User Charlie joined chatroom
[Alice] Sending: Hello everyone!
[Bob] Received from Alice: Hello everyone!
[Charlie] Received from Alice: Hello everyone!
[Bob] Sending: Hi Alice!
[Alice] Received from Bob: Hi Alice!
[Charlie] Received from Bob: Hi Alice!
The corresponding UML diagram is as follows

That is: users (colleagues) do not communicate directly, but instead send messages through the chat room (mediator). New users only need to register with the mediator without modifying other users’ code, reflecting the characteristic of “loose coupling”.
Example 2: Device Control Mediator (Smart Home Coordination)
Simulating a smart home system, the central control (mediator) coordinates the interaction of lights, curtains, and air conditioning (colleagues) (e.g., “close curtains automatically when lights are turned on”).
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Device types
typedef enum { DEVICE_LIGHT, DEVICE_CURTAIN, DEVICE_AC } DeviceType;
// Forward declaration
typedef struct Mediator Mediator;
typedef struct Device Device;
// Device (colleague) interface
typedef struct Device {
DeviceType type;
const char* name;
Mediator* mediator;
bool is_on; // Switch state
void (*turn_on)(struct Device* self);
void (*turn_off)(struct Device* self);
void (*receive)(struct Device* self, DeviceType sender, const char* event); // Receive mediator notification
} Device;
// Mediator interface
typedef struct Mediator {
Device* devices[5];
int count;
void (*register_device)(struct Mediator* self, Device* dev);
void (*notify)(struct Mediator* self, Device* sender, const char* event); // Notify other devices
} Mediator;
// Concrete device: Light
static void light_turn_on(Device* self) {
self->is_on = true;
printf("%s turned on\n", self->name);
self->mediator->notify(self->mediator, self, "light_on"); // Notify mediator of state change
}
static void light_turn_off(Device* self) {
self->is_on = false;
printf("%s turned off\n", self->name);
self->mediator->notify(self->mediator, self, "light_off");
}
static void light_receive(Device* self, DeviceType sender, const char* event) {
// Light receives events from other devices (e.g., no response when air conditioning is on)
(void)sender; (void)event;
}
Device* create_light(const char* name, Mediator* mediator) {
Device* dev = malloc(sizeof(Device));
dev->type = DEVICE_LIGHT;
dev->name = name;
dev->mediator = mediator;
dev->is_on = false;
dev->turn_on = light_turn_on;
dev->turn_off = light_turn_off;
dev->receive = light_receive;
return dev;
}
// Concrete device: Curtain
static void curtain_turn_on(Device* self) {
self->is_on = true; // Assume on means close curtains
printf("%s closed\n", self->name);
self->mediator->notify(self->mediator, self, "curtain_closed");
}
static void curtain_turn_off(Device* self) {
self->is_on = false; // off means open curtains
printf("%s opened\n", self->name);
self->mediator->notify(self->mediator, self, "curtain_opened");
}
static void curtain_receive(Device* self, DeviceType sender, const char* event) {
// Automatically close curtains when receiving the event that lights are on
if (sender == DEVICE_LIGHT && strcmp(event, "light_on") == 0) {
if (!self->is_on) { // If curtains are not closed, close them
self->turn_on(self);
}
}
}
Device* create_curtain(const char* name, Mediator* mediator) {
Device* dev = malloc(sizeof(Device));
dev->type = DEVICE_CURTAIN;
dev->name = name;
dev->mediator = mediator;
dev->is_on = false;
dev->turn_on = curtain_turn_on;
dev->turn_off = curtain_turn_off;
dev->receive = curtain_receive;
return dev;
}
// Concrete mediator: Smart Home Central Control
static void home_register(Mediator* self, Device* dev) {
if (self->count < 5) {
self->devices[self->count++] = dev;
printf("%s registered to home mediator\n", dev->name);
}
}
static void home_notify(Mediator* self, Device* sender, const char* event) {
// Forward event to all other devices
for (int i = 0; i < self->count; i++) {
Device* dev = self->devices[i];
if (dev != sender) {
dev->receive(dev, sender->type, event);
}
}
}
Mediator* create_home_mediator() {
Mediator* m = malloc(sizeof(Mediator));
m->count = 0;
m->register_device = home_register;
m->notify = home_notify;
return m;
}
// Free resources
void free_devices(Device** devs, int count) {
for (int i = 0; i < count; i++) free(devs[i]);
}
void free_home_mediator(Mediator* m) {
free(m);
}
// Client usage
int main() {
Mediator* home = create_home_mediator();
Device* light = create_light("Living Room Light", home);
Device* curtain = create_curtain("Living Room Curtain", home);
home->register_device(home, light);
home->register_device(home, curtain);
// When the light is turned on, the mediator notifies the curtains to close
printf("\nTurning on light...\n");
light->turn_on(light);
// Free resources
Device* devs[] = {light, curtain};
free_devices(devs, 2);
free_home_mediator(home);
return 0;
}
The output of the above code is as follows
Living Room Light registered to home mediator
Living Room Curtain registered to home mediator
Turning on light...
Living Room Light turned on
Living Room Curtain closed
The corresponding UML diagram is as follows

That is: devices interact through the central control (mediator), and when the light is turned on, there is no need to directly control the curtains; instead, the mediator forwards the event, and the curtains respond automatically. New devices (such as air conditioning) only need to implement the <span>receive</span> interface without modifying existing devices.
Example 3: Module Communication Mediator (Decoupling Software Modules)
Simulating a multi-module software system, asynchronous communication between modules is achieved through a message bus (mediator), avoiding direct dependencies between modules.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Message types
typedef enum { MSG_LOG, MSG_CONFIG, MSG_STATUS } MsgType;
// Message structure
typedef struct {
MsgType type;
const char* sender;
const char* data;
} Message;
// Forward declaration
typedef struct Mediator Mediator;
typedef struct Module Module;
// Module (colleague) interface
typedef struct Module {
const char* name;
Mediator* mediator;
void (*send_msg)(struct Module* self, MsgType type, const char* data); // Send message
void (*handle_msg)(struct Module* self, const Message* msg); // Handle message
} Module;
// Mediator (message bus) interface
typedef struct Mediator {
Module* modules[4];
int count;
void (*register_module)(struct Mediator* self, Module* mod);
void (*dispatch)(struct Mediator* self, const Message* msg); // Dispatch message
} Mediator;
// Concrete module: Log Module (handles LOG type messages)
static void log_send(Module* self, MsgType type, const char* data) {
Message msg = {.type = type, .sender = self->name, .data = data};
self->mediator->dispatch(self->mediator, &msg);
}
static void log_handle(Module* self, const Message* msg) {
if (msg->type == MSG_LOG) {
printf("[%s] Logging from %s: %s\n", self->name, msg->sender, msg->data);
}
}
Module* create_log_module(Mediator* mediator) {
Module* mod = malloc(sizeof(Module));
mod->name = "LogModule";
mod->mediator = mediator;
mod->send_msg = log_send;
mod->handle_msg = log_handle;
return mod;
}
// Concrete module: Config Module (handles CONFIG type messages)
static void config_send(Module* self, MsgType type, const char* data) {
Message msg = {.type = type, .sender = self->name, .data = data};
self->mediator->dispatch(self->mediator, &msg);
}
static void config_handle(Module* self, const Message* msg) {
if (msg->type == MSG_CONFIG) {
printf("[%s] Applying config from %s: %s\n", self->name, msg->sender, msg->data);
}
}
Module* create_config_module(Mediator* mediator) {
Module* mod = malloc(sizeof(Module));
mod->name = "ConfigModule";
mod->mediator = mediator;
mod->send_msg = config_send;
mod->handle_msg = config_handle;
return mod;
}
// Concrete mediator: Message Bus
static void bus_register(Mediator* self, Module* mod) {
if (self->count < 4) {
self->modules[self->count++] = mod;
printf("Module %s registered to bus\n", mod->name);
}
}
static void bus_dispatch(Mediator* self, const Message* msg) {
// Dispatch message to all modules, each module decides whether to handle it
for (int i = 0; i < self->count; i++) {
self->modules[i]->handle_msg(self->modules[i], msg);
}
}
Mediator* create_message_bus() {
Mediator* bus = malloc(sizeof(Mediator));
bus->count = 0;
bus->register_module = bus_register;
bus->dispatch = bus_dispatch;
return bus;
}
// Free resources
void free_modules(Module** mods, int count) {
for (int i = 0; i < count; i++) free(mods[i]);
}
void free_bus(Mediator* bus) {
free(bus);
}
// Client usage
int main() {
Mediator* bus = create_message_bus();
Module* log_mod = create_log_module(bus);
Module* config_mod = create_config_module(bus);
bus->register_module(bus, log_mod);
bus->register_module(bus, config_mod);
// Log module sends log message (config module does not handle)
printf("\nLog module sending log...\n");
log_mod->send_msg(log_mod, MSG_LOG, "System started");
// Config module sends config message (log module does not handle)
printf("\nConfig module sending config...\n");
config_mod->send_msg(config_mod, MSG_CONFIG, "max_conn=100");
// Free resources
Module* mods[] = {log_mod, config_mod};
free_modules(mods, 2);
free_bus(bus);
return 0;
}
The output of the above code is as follows
Module LogModule registered to bus
Module ConfigModule registered to bus
Log module sending log...
[LogModule] Logging from LogModule: System started
Config module sending config...
[ConfigModule] Applying config from ConfigModule: max_conn=100
The corresponding UML diagram is as follows

That is: modules send and receive messages through the message bus (mediator), and modules only need to focus on the message types they handle without knowing the existence of other modules, reducing direct dependencies between modules.
Example 4: Network Node Mediator (P2P Node Coordination)
Simulating a P2P network, the central node (mediator) coordinates the connections and data forwarding of multiple nodes (colleagues), avoiding the direct establishment of numerous connections between nodes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Node ID type
typedef int NodeId;
// Forward declaration
typedef struct Mediator Mediator;
typedef struct Node Node;
// Node (colleague) interface
typedef struct Node {
NodeId id;
Mediator* mediator;
void (*send)(struct Node* self, NodeId target, const char* data); // Send data
void (*receive)(struct Node* self, NodeId from, const char* data); // Receive data
} Node;
// Mediator (central node) interface
typedef struct Mediator {
Node* nodes[5];
int count;
Node* (*find_node)(struct Mediator* self, NodeId id); // Find node
void (*register_node)(struct Mediator* self, Node* node);
void (*route)(struct Mediator* self, NodeId from, NodeId to, const char* data); // Route data
} Mediator;
// Concrete node: P2P Node
static void node_send(Node* self, NodeId target, const char* data) {
printf("Node %d sending to %d: %s\n", self->id, target, data);
self->mediator->route(self->mediator, self->id, target, data); // Route through mediator
}
static void node_receive(Node* self, NodeId from, const char* data) {
printf("Node %d received from %d: %s\n", self->id, from, data);
}
Node* create_node(NodeId id, Mediator* mediator) {
Node* node = malloc(sizeof(Node));
node->id = id;
node->mediator = mediator;
node->send = node_send;
node->receive = node_receive;
return node;
}
// Concrete mediator: Central Routing Node
static Node* center_find(Mediator* self, NodeId id) {
for (int i = 0; i < self->count; i++) {
if (self->nodes[i]->id == id) {
return self->nodes[i];
}
}
return NULL;
}
static void center_register(Mediator* self, Node* node) {
if (self->count < 5) {
self->nodes[self->count++] = node;
printf("Node %d registered to center\n", node->id);
}
}
static void center_route(Mediator* self, NodeId from, NodeId to, const char* data) {
Node* target = self->find_node(self, to);
if (target) {
printf("Center routing from %d to %d\n", from, to);
target->receive(target, from, data);
} else {
printf("Center: Node %d not found\n", to);
}
}
Mediator* create_center_node() {
Mediator* center = malloc(sizeof(Mediator));
center->count = 0;
center->find_node = center_find;
center->register_node = center_register;
center->route = center_route;
return center;
}
// Free resources
void free_nodes(Node** nodes, int count) {
for (int i = 0; i < count; i++) free(nodes[i]);
}
void free_center(Mediator* center) {
free(center);
}
// Client usage
int main() {
Mediator* center = create_center_node();
Node* node1 = create_node(1, center);
Node* node2 = create_node(2, center);
Node* node3 = create_node(3, center);
center->register_node(center, node1);
center->register_node(center, node2);
center->register_node(center, node3);
// Node 1 sends data to Node 2 (through central routing)
printf("\n");
node1->send(node1, 2, "Hello Node2");
// Node 2 sends data to Node 3
printf("\n");
node2->send(node2, 3, "Hi Node3");
// Free resources
Node* nodes[] = {node1, node2, node3};
free_nodes(nodes, 3);
free_center(center);
return 0;
}
The output of the above code is as follows
Node 1 registered to center
Node 2 registered to center
Node 3 registered to center
Node 1 sending to 2: Hello Node2
Center routing from 1 to 2
Node 2 received from 1: Hello Node2
Node 2 sending to 3: Hi Node3
Center routing from 2 to 3
Node 3 received from 2: Hi Node3
The corresponding UML diagram is as follows

That is: P2P nodes route data through the central node (mediator), and nodes do not need to know the location or connection method of other nodes; they only need to send the target ID through the mediator, which is suitable for large-scale distributed systems.
Example 5: UI Component Mediator (Interface Control Interaction)
Simulating a GUI interface, the window (mediator) coordinates the interaction of buttons, text boxes, and labels (colleagues) (e.g., “update text box content after button click”).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Event types
typedef enum { EVENT_CLICK, EVENT_TEXT_CHANGE } EventType;
// Forward declaration
typedef struct Mediator Mediator;
typedef struct Component Component;
// UI component (colleague) interface
typedef struct Component {
const char* name;
Mediator* mediator;
void (*trigger)(struct Component* self, EventType type, const char* data); // Trigger event
void (*update)(struct Component* self, const char* data); // Update component
} Component;
// Mediator (window) interface
typedef struct Mediator {
Component* components[3];
int count;
void (*add_component)(struct Mediator* self, Component* comp);
void (*on_event)(struct Mediator* self, Component* sender, EventType type, const char* data); // Handle event
} Mediator;
// Concrete component: Button
static void button_trigger(Component* self, EventType type, const char* data) {
printf("Button %s triggered event: %s\n", self->name, data);
self->mediator->on_event(self->mediator, self, type, data);
}
static void button_update(Component* self, const char* data) {
printf("Button %s updated: %s\n", self->name, data);
}
Component* create_button(const char* name, Mediator* mediator) {
Component* btn = malloc(sizeof(Component));
btn->name = name;
btn->mediator = mediator;
btn->trigger = button_trigger;
btn->update = button_update;
return btn;
}
// Concrete component: Text Box
static void textbox_trigger(Component* self, EventType type, const char* data) {
printf("Textbox %s triggered event: %s\n", self->name, data);
self->mediator->on_event(self->mediator, self, type, data);
}
static void textbox_update(Component* self, const char* data) {
printf("Textbox %s content set to: %s\n", self->name, data);
}
Component* create_textbox(const char* name, Mediator* mediator) {
Component* tb = malloc(sizeof(Component));
tb->name = name;
tb->mediator = mediator;
tb->trigger = textbox_trigger;
tb->update = textbox_update;
return tb;
}
// Concrete mediator: Window
static void window_add(Mediator* self, Component* comp) {
if (self->count < 3) {
self->components[self->count++] = comp;
printf("Component %s added to window\n", comp->name);
}
}
static void window_on_event(Mediator* self, Component* sender, EventType type, const char* data) {
// Button click event: update text box content
if (type == EVENT_CLICK && strstr(sender->name, "Button")) {
// Find text box and update
for (int i = 0; i < self->count; i++) {
Component* comp = self->components[i];
if (strstr(comp->name, "Textbox")) {
comp->update(comp, data);
}
}
}
}
Mediator* create_window() {
Mediator* win = malloc(sizeof(Mediator));
win->count = 0;
win->add_component = window_add;
win->on_event = window_on_event;
return win;
}
// Free resources
void free_components(Component** comps, int count) {
for (int i = 0; i < count; i++) free(comps[i]);
}
void free_window(Mediator* win) {
free(win);
}
// Client usage
int main() {
Mediator* window = create_window();
Component* btn = create_button("SubmitButton", window);
Component* tb = create_textbox("InputTextbox", window);
window->add_component(window, btn);
window->add_component(window, tb);
// Click button, mediator notifies text box to update content
printf("\nClicking button...\n");
btn->trigger(btn, EVENT_CLICK, "Submitted!");
// Free resources
Component* comps[] = {btn, tb};
free_components(comps, 2);
free_window(window);
return 0;
}
The output of the above code is as follows
Component SubmitButton added to window
Component InputTextbox added to window
Clicking button...
Button SubmitButton triggered event: Submitted!
Textbox InputTextbox content set to: Submitted!
The corresponding UML diagram is as follows

That is: UI components respond to events through the window (mediator), and button clicks do not require direct manipulation of the text box; instead, the mediator coordinates, and new components (such as labels) only need to register with the window and implement <span>update</span>, without modifying existing components.
4. Application of the Mediator Pattern in the Linux Kernel
1. ** VFS (Virtual File System) **: VFS acts as a mediator between user space and specific file systems (such as ext4, btrfs). Users operate files through unified interfaces like <span>open</span> and <span>read</span>, while VFS forwards requests to specific file systems, hiding the implementation differences of different file systems, allowing applications to not worry about the specific format of file storage.
2. ** Core Device Model (kobject) **: In the kernel device model, <span>kobject</span> serves as a mediator between devices, drivers, and buses, managing similar objects through <span>kset</span>, providing event notifications (<span>kobject_uevent</span>), attribute access (<span>sysfs</span>), and other functions, coordinating the matching process between devices and drivers, avoiding direct interaction between devices and drivers.
3. ** Network Protocol Stack (netfilter) **: netfilter is a mediation framework for network packet processing, intercepting packets through hook functions (such as <span>NF_INET_PRE_ROUTING</span>) and coordinating modules such as firewalls, NAT, and traffic control for packet processing. Packets do not need to know the specific processing modules, as netfilter forwards them to the corresponding modules according to rules.
4. ** Interrupt Controller (IRQ controller) **: The interrupt controller acts as a mediator between the CPU and peripheral interrupt sources. When a peripheral generates an interrupt, it first sends it to the interrupt controller, which sorts them by priority and forwards them to the CPU, reducing the coupling between the CPU and peripherals by not requiring the CPU to handle each peripheral’s interrupt signal directly.
5. ** System Calls (syscall) **: The system call mechanism serves as a mediator between user space programs and kernel functions. User space triggers kernel calls through the <span>syscall</span> instruction, and the kernel’s system call table (<span>sys_call_table</span>) forwards requests to specific kernel functions (such as <span>sys_read</span>), allowing user programs to not know the specific implementation of kernel functions.
5. Implementation Considerations
1. ** Complexity Control of the Mediator **: The mediator centrally manages all interaction logic, which may lead to it becoming too large (the “God Object” problem). It is necessary to split the mediator’s responsibilities (e.g., by dividing into multiple sub-mediators by function) or limit the mediator to only handle core coordination logic.
2. ** Dependency of Colleagues on the Mediator **: Colleagues need to hold a pointer to the mediator, so the mediator’s interface should remain stable to avoid frequent modifications that require all colleagues to recompile.
3. ** Thread Safety **: In a multi-threaded environment, methods like the mediator’s <span>notify</span> / <span>dispatch</span> need to protect shared data (such as the colleague list) through locking mechanisms to avoid inconsistencies caused by concurrent modifications.
4. ** Performance Considerations **: The forwarding logic of the mediator introduces additional overhead, and for high-frequency interaction scenarios (such as kernel interrupt handling), it is necessary to optimize the mediator’s lookup and forwarding efficiency (e.g., using hash tables for quick target colleague location).
5. ** Avoid Over-Design **: If interactions between objects are simple (e.g., one-to-one communication), there is no need to introduce a mediator, as it would increase system complexity. The mediator is suitable for many-to-many interaction scenarios.
6. Additional Notes
- • Difference between Mediator Pattern and Observer Pattern: The Mediator Pattern emphasizes coordination of interactions among multiple objects, while the Observer Pattern emphasizes one-to-many notification mechanisms (observers passively receive notifications without interaction logic).
- • Extension of the Mediator Pattern: Combined with the Command Pattern, the interaction requests of colleagues can be encapsulated as commands, executed in order by the mediator, supporting request queuing and undoing.
- • Applicable Scenarios: Multi-module communication, coordination in distributed systems, UI component interaction, device linkage, protocol conversion, and other scenarios with complex many-to-many interactions.
Through the Mediator Pattern, C language programs (especially in the Linux kernel) can centrally manage complex multi-object interaction logic, significantly reducing the coupling between objects, making the system easier to maintain and extend. Core components in the kernel, such as VFS and kobject, achieve efficient cross-module collaboration through the Mediator Pattern.
Click to read the full article, thank you for sharing, saving, liking, and viewing