avatarItchimonji

Summary

The Mediator pattern is a design pattern that facilitates communication between objects by centralizing the interaction logic, thus reducing coupling and improving the manageability of complex interactions.

Abstract

The Mediator pattern is categorized as a behavioral design pattern and is exemplified by a chat room where a server acts as the mediator to manage user interactions and message distribution without users directly referencing each other. This pattern promotes loose coupling by preventing direct object references, allowing for easier reusability and adaptability of objects. It centralizes control of interactions, which simplifies object management and abstraction of cooperation but can lead to a complex mediator. The pattern is suitable for systems with well-defined but complex object interactions, where objects need to communicate in a many-to-many fashion without being tightly coupled.

Opinions

  • The Mediator pattern is praised for its ability to decouple participant objects, enabling them to be varied and reused independently.
  • It simplifies object control by transforming many-to-many interactions into one-to-many interactions between the mediator and the participant objects.
  • The pattern abstracts the cooperation between objects, which can make the system easier to understand, extend, and maintain.
  • Centralized control is seen as both an advantage and a potential drawback, as it can lead to a monolithic mediator that becomes complex and difficult to manage.
  • The pattern is considered particularly useful in scenarios where distributed behavior

Mediator | Cheat Sheet

Behavioral Pattern — Design Patterns Series

A Mediator controls the interaction of different object groups. A real life example is a chat room in which different users write to each other. The chat room or server is the Mediator object that manages users and distributes the messages to different clients.

The pattern helps to loose coupling by preventing explicit referencing of participants. The user in a chat room does not know the direct reference of another user. She/he only knows the server and another user’s id. The responsibility of the Mediator is to forward messages to the correct receiver. So, hundreds of users can be in a room and all objects are loosely coupled.

This causes the Mediator to have individual control of all interactions of the users / object groups — a centralized control.

Real-life examples

  • Air traffic control through tower
  • Chat room (websockets communicate with server; server is Mediator object)
  • Taxi radio (call centre is Mediator object)
  • Mapper in Enterprise Applications (separates different subsystems that should / want to communicate with each other)
  • Framework examples like VerneMQ, Kafka

Meaning

  • Definition of an object that controls the interaction of object groups
  • Mediator favors loose coupling by preventing explicit referencing of participants and allowing individual control of their interactions

Applicability (suitable if …)

  • Communication within an object group is well-defined, but also in a complex way (dependencies are unstructured and difficult to understand)
  • Reusability of objects is difficult because it references and communicates with numerous other objects
  • Adaptability of distributed behavior across several classes is supposed to be ensured

Assets and Drawbacks

  • Restricted subclassing of the mediator for behavior change
  • Decoupling of participant objects — this allows them to be varied and reused independently of each other
  • Simplification of object control (n:n becomes 1:n) — Easier object management (understanding, expansion, and more)
  • Abstraction of object cooperation
  • Centralised control — complexity of interactions is exchanged for complexity of the mediator (monolithic approach)

Example

A chat room is a good real life example where the Mediator pattern comes into play. Different users communicate with a server — and this server is the Mediator which distributes the messages to different user. So, users can communicate through the server, and they can be added to a room and can get notified.

The chat room (Mediator) manages all users and sends messages between different users with a notify function.

A user can send and receive messages. Also, the user knows his Mediator object.

If the user wants to interact with another user, she/he sends the user id and a message to the Mediator (server), which then forwards the message to the user with the given id.

The client only needs to initiate an object of a chat room and add users who want to write each other.

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Behavioral Patterns deal with algorithms and assignment of responsibilities to objects. Furthermore, they describe mutual communication patterns and grasp complex program sequences. They are therefore very well suited for use in areas where complicated code needs to be made readable and maintainable. Extensions are, in consequence, very good and quickly implementable.

Code example on Github

Learn More

Resources

Design Patterns
Behavioral Patterns
Mediator
Object Composition
Cheatsheet
Recommended from ReadMedium