Skip to main content

Mediator Design Pattern

Structural: Mediator Pattern

Can you explain the mediator design pattern?

View Answer:
Interview Response: The Mediator Pattern is a design pattern that allows one item to notify another group of objects when an event or action occurs. The Mediator and Observer patterns vary in that the Mediator pattern allows one object to be alerted of events occurring in other objects. In contrast, the Observer pattern allows one object to subscribe to numerous events occurring in other objects.

Code Example:

The objects participating in this pattern are:

Mediator -- example code: Chatroom

  • It specifies an interface for interacting with Colleague objects
  • Maintains references to Colleague objects
  • Manages central control over operations

Colleagues -- example code: Participants

  • objects that the Mediator is mediating
  • each instance maintains a connection (reference) to the Mediator

let Participant = function (name) { = name;
this.chatroom = null;

Participant.prototype = {
send: function (message, to) {
this.chatroom.send(message, this, to);
receive: function (message, from) {
console.log( + ' to ' + + ': ' + message);

let Chatroom = function () {
let participants = {};

return {
register: function (participant) {
participants[] = participant;
participant.chatroom = this;

send: function (message, from, to) {
if (to) {
// single message
to.receive(message, from);
} else {
// broadcast message
for (key in participants) {
if (participants[key] !== from) {
participants[key].receive(message, from);

function run() {
let yoko = new Participant('Yoko');
let john = new Participant('John');
let paul = new Participant('Paul');
let ringo = new Participant('Ringo');

let chatroom = new Chatroom();

yoko.send('All you need is love.');
yoko.send('I love you John.');
john.send('Hey, no need to broadcast', yoko);
paul.send('Ha, I heard that!');
ringo.send('Paul, what do you think?', paul);




Yoko to John: All you need is love.
Yoko to Paul: All you need is love.
Yoko to Ringo: All you need is love.
Yoko to John: I love you John.
Yoko to Paul: I love you John.
Yoko to Ringo: I love you John.
John to Yoko: Hey, no need to broadcast
Paul to Yoko: Ha, I heard that!
Paul to John: Ha, I heard that!
Paul to Ringo: Ha, I heard that!
Ringo to Paul: Paul, what do you think?


The Mediator pattern belongs to which pattern category?

View Answer:
Interview Response: The Mediator pattern is a type of behavioral design pattern.

When should you utilize the JavaScript Mediator Pattern?

View Answer:
Interview Response: We can use it:

  • If your system has several components that must communicate with one another.
  • To avoid tight object coupling in a system with many objects.
  • To enhance code readability.
  • To make it easier to maintain code.
  • If communication between objects gets complicated or impedes code reusability

What are some of the advantages of employing the Mediator pattern?

View Answer:
Interview Response: Benefits of the Mediator Pattern

  • Singular Responsibility Principle -- You may consolidate the communications between numerous components into a single location, making them easier to understand and maintain.
  • The Open/Closed Principle You can add new mediators without changing the key components.
  • You can reduce coupling between software components.
  • We can use individual components more efficiently.

What are some of the Mediator pattern's drawbacks?

View Answer:
Interview Response: Drawbacks of the Iterator Pattern.

  • A mediator can evolve into a God Object over time.