Let's look at an example: consider how you want to create two applications:
- Chat application.
- Ambulance operator app.
mediator
Once you create the chat application, you will select the mediator design template.
- People can join and leave the chat at any time, so it makes no sense to keep a direct link between two people in the chat.
- We still need to facilitate communication between the two people and allow them to chat.
Why do we prefer a mediator ? just take a look at its definition:
Using the mediator template, the connection between objects is encapsulated inside an intermediary object. Objects no longer communicate directly with each other, but instead communicate through an intermediary. This reduces the relationship between communicating objects, thereby reducing traction.
How does magic work? First, we will create a chat broker and make object objects for it, so it will have a two-way connection with each person (a person can send a message using the chat broker because he has access to it, and the chat broker will access the received object method -man also causes him access to it)
function Person(name) { let self = this; this._name = name; this._chat = null; this._receive(from, message) { console.log("{0}: '{1}'".format(from.name(), message)); } this._send(to, message) { this._chat.message(this, to, message); } return { receive: (from, message) => { self._receive(from, message) }, send: (to, message) => { self._send(to, message) }, initChat: (chat) => { this._chat = chat; }, name: () => { return this._name; } } } function ChatMediator() { let self = this; this._persons = []; return { message: function (from, to, message) { if (self._persons.indexOf(to) > -1) { self._persons[to].receive(from, message); } }, register: function (person) { person.initChat(self); self._persons.push(person); } unRegister: function (person) { person.initChat(null); delete self._persons[person.name()]; } } };
observer
When you create the 911 challenge application, you select the observer design template.
- Each ambulance
observer object wants to be informed when there is an emergency condition, so it can manage the address and give help. observable emergency operator keeps a link to each of the ambulance observers and notifies them when help is needed (or generating an event).
Why would we prefer an observer ? just take a look at its definition:
An object called a subject maintains a list of its dependents, called observers, and automatically notifies them of any state changes, usually by calling one of its methods.
function AmbulanceObserver(name) { let self = this; this._name = name; this._send(address) { console.log(this._name + ' has been sent to the address: ' + address); } return { send: (address) => { self._send(address) }, name: () => { return this._name; } } } function OperatorObservable() { let self = this; this._ambulances = []; return { send: function (ambulance, address) { if (self._ambulances.indexOf(ambulance) > -1) { self._ambulances[ambulance].send(address); } }, register: function (ambulance) { self._ambulances.push(ambulance); } unRegister: function (ambulance) { delete self._ambulances[ambulance.name()]; } } };
Differences:
mediator chat mediator has two-way communication between objects of persons (sending and receiving), where the observable operator has only one-way communication (he informs the ambulance observer about control and completion).mediator chat mediator can cause people’s objects to interact between them (even if it’s not a direct connection) first aid observers only record observable operator events.- Each object of the object has a link to the chat
mediator , and the chat mediator maintains a link to each person. By observing that the ambulance observer does not pay attention to the observable operator, only the observable operator maintains a link to each ambulance observer .