-
Notifications
You must be signed in to change notification settings - Fork 3
Mediator Pattern
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.
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:
- A sender doesn't have to be registered just the receiver.
- Anyone can send a message, it is up to the developer to decide when and how to execute a function.
- A sender can send messages to himself.
- 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.
participants
(type: Object) Registered participants.
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.
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'
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.