Carl Danley

The Mediator Pattern

Posted by Carl Danley on .
Featured

The Mediator Pattern

Posted by Carl Danley on .

The American Heritage dictionary defines the word mediator as "one that mediates, especially one that reconciles differences between disputants." In this manner, the mediator pattern usually implements a single object that becomes a shared resource through all of the different pieces of an application. It's a higher-level version of pub/sub in that it's commonly used to communicate across the different features of an application in contrast to being used within one feature to communicate with all of the individual pieces of that same feature.

"Understanding the similarities and differences between an event aggregator and mediator is important for semantic reasons."
- Derick Bailey

This article is part of a series called JavaScript Design Patterns.

Advantages

  • Reduces the communication relationship from "many-to-many" to "many-to-one"
  • Helps us pinpoint dependencies
  • Excellent at decoupling objects which often promotes smaller, reusable components

Disadvantages

  • Introduces a single point of failure
  • When modules communicate back and forth using a mediator pattern, it tends to become cumbersome and usually results in a clear performance hit. It's best when the mediator is only used to coordinate actions across multiple features and not for communication within the individual features themselves; keep the airways clean!

Example

Additional Resources

Other Common JS Patterns