javascript design patterns (intermediary model)

The role of intermediary model is tightly coupled to lift the relationship between the object and the object, it is also known as 'mediator'. All objects are objects to communicate through intermediaries, rather than referring to each other, so when an object is changed, you only need to notify the mediator.

Such as: airport control tower, each aircraft control tower and only need to communicate, control tower know each aircraft flight conditions, taking off and landing can arrange all the time, to adjust routes, etc.

Intermediary model in line with the Law of Demeter, namely the principle of a minimum of knowledge, refers to an object should be as little understanding of other objects. If the coupling between objects is too high, to change an object, it will affect many objects, difficult to maintain. When the object is very tight coupling, an object to be modified without affecting the other objects is very difficult.

If the complex coupling between the object does result in a call and maintain experienced difficulties, but these changes with the degree of coupling of the project has grown exponentially, then we can consider the use of intermediary model to reconstruct the code! Mediator by decoupling to improve code maintainability.

// play home objects are created by the Player () constructor has its own name and attribute points. Play the prototype () method is responsible to add more minutes and then a notification broker: 
function Player (name) {
     the this .points = 0 ;
     the this .name = name; 
} 
Player.prototype.play = function () {
     the this .points =. 1 + ; 
    mediator.played (); 
};
// coreboard objects (scoreboard) has an update () method, it will call in a mediator after each player comes to an end. Total analysis did not know anything about the players, not save scores, it is responsible for displaying a mediator to come fraction 
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>' ;
        .element.innerHTML = msg;
    }
};
// Now we look at objects mediator (mediator). When the game initialization, create a player in the setup () method, and then put the players after the property for later use. played () method will be a player in each round comes to an end after the call, 
// it updates the score and then hash table and then passed it on scoreboard for display. The last method is KeyPress (), is responsible for processing keyboard events, decide who is played by the player, and notifies it
var Mediator = { Players: {}, Setup: function () { var Players = the this .players; players.home = new new Player ( 'Home' ); players.guest = new new Player ( 'the Guest' ); }, Played: function () { var Players = the this .players, Score= { Home: players.home.points, Guest: players.guest.points }; scoreboard.update(score); }, keypress: function (e) { e = e || window.event; // IE if (e.which === 49) { // key "1" mediator.players.home.play(); return; } if (e.which === 48) { // key "0" mediator.players.guest.play(); return; } } };
And the game is initialized
 // Go! 
Mediator.setup (); 
window.onkeypress = mediator.keypress; 

// Game over 30 seconds The in 
the setTimeout ( function () { 
    window.onkeypress = null ; 
    Alert ( 'Game over!' ); 
}, 30000);

Reprinted from https://www.cnblogs.com/susufufu/p/5808433.html

Guess you like

Origin www.cnblogs.com/xingxingclassroom/p/11359592.html