1. Overall summary
1. The author briefly talks about it
Let’s give a simple example from our daily life. When we go to a real estate agency to rent a house, the real estate agency forms an intermediary between the tenant and the landlord’s lessor. The renter doesn't care who he rents. The landlord’s lessor doesn’t care who he rents to. This transaction became so convenient because of the existence of an intermediary.
During the software development process, there will inevitably be a situation where multiple classes or subsystems interact with each other, and the interaction is very complicated, resulting in each class having to know the classes it needs to interact with, so that their coupling will appear extremely powerful. The whole body is affected by one move, and the consequences are very serious, and the big bear is very angry! ~~~~(>_<)~~~~
Okay, since the question has been raised, please invite our protagonist in this issue - the intermediary model to appear.
The function of the intermediary is to encapsulate the interaction between objects. If an object's operations will cause changes to other related objects, and this object does not want to handle these relationships by itself, then you can find an intermediary and let it handle these troublesome relationships. See the following small example:
The code copy is as follows:
var Participant = function(name) {
this.name = name;
this.chatroom = null;
};
Participant.prototype = {
send: function(message, to) {
this.chatroom.send(message, this, to);
},
receive: function(message, from) {
log.add(from.name + " to " + this.name + ": " + message);
}
};
var Chatroom = function() {
var participants = {};
return {
register: function(participant) {
participants[participant.name] = participant;
participant.chatroom = this;
},
send: function(message, from, to) {
if (to) {
to.receive(message, from);
} else {
for (key in participants) {
if (participants[key] !== from) {
participants[key].receive(message, from);
}
}
}
}
};
};
var log = (function() {
var log = "";
return {
add: function(msg) { log += msg + "/n"; },
show: function() { alert(log); log = ""; }
}
})();
function run() {
var yoko = new Participant("Yoko");
var john = new Participant("John");
var paul = new Participant("Paul");
var ringo = new Participant("Ringo");
var chatroom = new Chatroom();
chatroom.register(yoko);
chatroom.register(john);
chatroom.register(paul);
chatroom.register(ringo);
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 hear that!");
ringo.send("Paul, what do you think?", paul);
log.show();
}
In the sample code we have four participants, join the chat session by registering a chat room (mediary). Representative of participants for each participant. Participants send messages to each other and process routes to chat rooms.
The chat room objects here play the role of intermediary, coordinate other objects, organize them reasonably, and reduce coupling.
2. Source code case reference
We should be very familiar with the MVC three-layer model solid model (Model), view presentation layer (View) and control layer (Control/Mediator).
The control layer is the intermediary between the presentation layer and the model layer. Generally speaking, MVC is also an application of the intermediary model in framework design.
3. Case introduction
The code copy is as follows:
function Player(name) {
this.points = 0;
this.name = name;
}
Player.prototype.play = function () {
this.points += 1;
mediator.played();
};
var scoreboard = {
element:document.getElementById('results'),
update:function (score) {
var i, msg = '';
for (i in score) {
if (score.hasOwnProperty(i)) {
msg += '<p><strong>' + i + '<//strong>: ';
msg += score[i];
msg += '<//p>';
}
}
this.element.innerHTML = msg;
}
};
var mediator = {
players:{},
setup:function () {
var players = this.players;
players.home = new Player('Home');
players.guest = new Player('Guest');
},
played:function () {
var players = this.players,
score = {
Home:players.home.points,
Guest:players.guest.points
};
scoreboard.update(score);
},
keypress:function (e) {
e = e || window.event;
if (e.which === 49) {
mediator.players.home.play();
return;
}
if (e.which === 48) {
mediator.players.guest.play();
return;
}
}
};
mediator.setup();
window.onkeypress = mediator.keypress;
setTimeout(function () {
window.onkeypress = null;
console.log('Game over!');
}, 30000);
Four, let's summarize
Why Mediator?
There are many interactions between objects. Each object's behavioral operations depend on each other to modify the behavior of one object, and at the same time it involves modifying the behavior of many other objects.
If you use the Mediator mode, the coupling between each object can be loosened. You only need to care about the relationship with the Mediator, and turn the many-to-many relationship into a one-to-many relationship.
It can reduce the complexity of the system and improve the modifiable scalability.
Occasions using intermediary model
1. A well-defined set of objects that now require complex communication.
2. Customize a behavior distributed in multiple classes without wanting to generate too many subclasses.
It can be seen that the mediation objects are mainly used to encapsulate behavior, and the participants in the behavior are those objects, but through the mediator, these objects do not need to know each other. (Specific implementation of Dimit's law)
Advantages of using the intermediary model:
1. Reduces the coupling between system objects, making the objects easy to be reused independently.
2. Improve the flexibility of the system and make the system easy to expand and maintain.
Disadvantages of using the intermediary model:
The disadvantages of the intermediary model are obvious, because this "intermediary" assumes more responsibilities, so once there is a problem with the intermediary object, the entire system will be greatly affected.