Flutter Community
Published in

Flutter Community

Flutter Design Patterns: 22 — Mediator

An overview of the Mediator design pattern and its implementation in Dart and Flutter

Table of Contents

  • What is the Mediator design pattern?
  • Analysis
  • Implementation
  • Other articles in this series
  • Your contribution

What is the Mediator design pattern?

The importance of a mediator (source)

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

TL;DR: the main target of the Mediator design pattern is to move from the communicating object dependencies chaos provided on the left to the decoupled one provided on the right:

Object dependencies before (left) and after (right) using the Mediator design pattern


The general structure of the Mediator design pattern looks like this:

Structure of the Mediator design pattern (source)
  • ConcreteMediator — encapsulates relations between components by containing references to them;
  • (Optional) Abstract Component or Component Interface — similar communicating components could implement the same interface or extend the same base class. In this case, ConcreteMediator could store a list of components extending/implementing this class instead of keeping multiple references as separate properties;
  • Concrete component or Colleague — contains a reference to a mediator. Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague (component).


The Mediator design pattern should be used when instead of having tightly coupled classes you want to have loose-coupled ones, because:


Class diagram

The class diagram below shows the implementation of the Mediator design pattern:

Class Diagram — Implementation of the Mediator design pattern
  • send() — sends a notification;
  • sendTo<T>() — sends a notification to specific team members.
  • register() — registers a team member to the hub;
  • send() — sends a notification to registered team members;
  • sendTo<T>() — sends a notification to specific registered team members.


An abstract class implementing base methods for all the specific team member classes. Method receive() sets the lastNotification value, send() and sendTo<T>() methods send notification by using the corresponding notificationHub methods.


Concrete team member classes

All of the specific team member classes extend the TeamMember and override the default toString() method.



An abstract class that defines abstract methods to be implemented by specific notification hub classes. Method getTeamMembers() returns a list of registered team members to the hub, and register() registers a new member to the hub. Method send() sends the notification to all the registered team members to the hub (excluding sender) while sendTo<T>() sends the notification to team members of a specific type (excluding sender).



A specific notification hub implementing abstract NotificationHub methods. The class also contains private teamMembers property — a list of registered team members to the hub.



First of all, a markdown file is prepared and provided as a pattern’s description:


Your contribution

👏 Press the clap button below to show your support and motivate me to write better!
💬 Leave a response to this article by providing your insights, comments or wishes for the series.
📢 Share this article with your friends, colleagues on social media.
➕ Follow me on Medium.
⭐ Star the Github repository.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store