Skip to content

Mediator and Middleware Pattern

In software architecture, managing interactions between components efficiently is crucial for scalability and maintainability. Two key concepts, the Mediator pattern and Middleware, provide structured approaches to handle these interactions, each serving distinct roles in software development.

Mediator Pattern

The Mediator pattern is a behavioral design pattern where a central component (the Mediator) facilitates communication between other components (colleagues). Instead of components interacting directly, they communicate through the Mediator, promoting loose coupling and simplifying maintenance by centralizing communication logic within the application.

Middleware

Middleware refers to software that bridges communication and manages interactions between different applications or systems. It operates at a lower level than design patterns like the Mediator, focusing on integration, data management, and interoperability across distributed systems.

Table of Contents

Implementation

Example: Chatroom

Imagine a chatroom where users communicate. Instead of users directly messaging each other, all messages go through the chatroom (the Mediator). Each user sends messages to the chatroom, which then distributes them to the appropriate recipients. This way, the Mediator ensures organized and controlled communication.

javascript
class ChatRoom {
  logMessage(user, message) {
    const time = new Date();
    const sender = user.getName();
    console.log(`${time} [${sender}]: ${message}`);
  }
}

class User {
  constructor(name, chatroom) {
    this.name = name;
    this.chatroom = chatroom;
  }

  getName() {
    return this.name;
  }

  send(message) {
    this.chatroom.logMessage(this, message);
  }
}

// Example usage:
const chatroom = new ChatRoom();
const user1 = new User("John Doe", chatroom);
const user2 = new User("Jane Doe", chatroom);

user1.send("Hi there!");
user2.send("Hey!");

Use Cases

1. Chat Application:

  • Scenario: Users communicate in a chatroom.

  • Implementation: Messages are sent through a central chatroom (Mediator) which distributes them to recipients, ensuring organized communication without direct user-to-user interaction.

2. Air Traffic Control System:

  • Scenario: Aircraft communicate positions and intentions.

  • Implementation: An Air Traffic Controller (Mediator) manages communication between aircraft, ensuring coordinated air traffic management and safety.

3. GUI Components:

  • Scenario: Interactive graphical user interface (GUI) elements.

  • Implementation: A GUI framework uses a Mediator to handle user interactions, updating components based on user actions to maintain UI consistency.

Pros and Cons

Pros

  • Decoupling: Components interact with each other through the Mediator, reducing direct dependencies and promoting loose coupling.
  • Centralized Control: The Mediator provides a single point of control for communication, making it easier to manage interactions and behavior.
  • Enhanced Maintainability: Changes to how components interact are confined to the Mediator, reducing the risk of unintended side effects across the system.

Cons

  • Complexity: Introducing a Mediator can add complexity, especially if not designed carefully. It's essential to strike a balance between centralized control and maintaining simplicity.
  • Single Point of Failure: Since the Mediator manages communication, any issues with the Mediator can affect the entire system's functionality.

Conclusion

The Mediator pattern is a powerful tool for managing interactions between components in a system. By centralizing communication through a Mediator, you can achieve better decoupling, centralized control, and enhanced maintainability. However, it's important to be mindful of the potential complexity and the risk of creating a single point of failure. When implemented thoughtfully, the Mediator pattern can significantly improve the structure and maintainability of your software.

Made By Sourav Bandyopadhyay