Skip to content

Mediator Pattern

Pablo Garcia edited this page Jul 22, 2017 · 19 revisions

The dictionary refers to a mediator as a neutral party that assists in negotiations and conflict resolution. In our world, a mediator is a behavioral design pattern that allows us to expose a unified interface through which the different parts of a system may communicate.

If it appears a system has too many direct relationships between components, it may be time to have a central point of control that components communicate through instead. The Mediator promotes loose coupling by ensuring that instead of components referring to each other explicitly, their interaction is handled through this central point. This can help us decouple systems and improve the potential for component reusability.

- Learning JavaScript Design Patterns

In go-patterns the implementation of the Mediator avoids the use of some restrictions to increase flexibility and usability of the pattern across multiple scenarios. For example:

  1. A sender doesn't have to be registered just the receiver.
  2. Anyone can send a message, it is up to the developer to decide when and how to execute a function.
  3. A sender can send messages to himself.
  4. If a receiver doesn't exist, it fails silently (does not throw an error).

If you wish to modify the behavior of the pattern, you can do so by looking at the Customizing Patterns advanced topic.

How to use

Instance Properties

participants (type: Object) Registered participants.

Instance Methods.

register(name, method) Adds a participant to the list.

  • name (type: String) Name of the participant to register.
  • method (type: Function) Method that is to be executed upon a message sent to that particular participant.

send(options) Sends a message to a specific registered participant.

  • options (type: Object)
  • options.message (type: Anything) Data to be sent to the participants.
  • options.from (type: String) Name of the sender.
  • options.to (type: String) Name of the receiver.

broadcast(options) Sends a "message" to all registered participants.

  • options (type: Object)
  • options.message (type: Anything) Data to be sent to the participants.
  • options.from (type: String) Name of the registered participant.

count() Returns the number of registered participants.

Examples

Basic Use Case

The most basic implementation, of all the patterns, do not require any options.

var patterns = require('go-patterns');

var Mediator = patterns.mediator().build();
var mediator = new Mediator();

mediator.register('toA', (msg, sender) => console.log(msg, sender));
mediator.register('toB', (msg, sender) => console.log(msg, sender));

mediator.send({
  message: { key: 'hi a' },
  from: 'fromB',
  to: 'toA'
}); // => { key: 'value 1' } 'fromB'
mediator.send({
  message: { key: 'hi b' },
  from: 'fromA',
  to: 'toB'
}); // => { key: 'value 2' } 'fromA'
mediator.send({
  message: { key: 'hi c' },
  from: 'fromA',
  to: 'toC'
}); // C doesn't exist

mediator.broadcast({
  message: { key: 'hi everyone' },
  from: 'fromA'
}); // A and B get => { key: 'hi everyone' } 'fromA'

Verbose Use Case

var patterns = require('go-patterns');

var OrgChat = patterns.mediator({
  constructor: function() {
    this.name = "Organization Chat";
  },
  publics: {
    unregister(name) {
      delete this.participants[name];
    }
  }
}).build();
var orgChat = new OrgChat();

orgChat.register('Human', (msg, sender) => console.log(msg, sender));
orgChat.unregister('Human');
orgChat.broadcast({
  message: 'Is anyone there?',
  from: 'God'
}); // no log.