一,總體概要
1,筆者淺談
我們從日常的生活中打個簡單的比方,我們去房屋中介租房,房屋中介人在租房者和房東出租者之間形成一條中介。租房者並不關心他租誰的房。房東出租者也不關心他租給誰。因為有中介的存在,這場交易才變得如此方便。
在軟件的開發過程中,勢必會碰到這樣一種情況,多個類或多個子系統相互交互,而且交互很繁瑣,導致每個類都必須知道他需要交互的類,這樣它們的耦合會顯得異常厲害。牽一發而動全身,后果很嚴重,大熊很生氣!~~~~(>_<)~~~~
好了,既然問題提出來了,那有請我們這期的主角------中介者模式出場吧
中介者的功能就是封裝對象之間的交互。如果一個對象的操作會引起其他相關對象的變化,而這個對象又不希望自己來處理這些關系,那么就可以去找中介者,讓它來處理這些麻煩的關系。看下面的小例子:
1 var Participant = function(name) { 2 this.name = name; 3 this.chatroom = null; 4 }; 5 Participant.prototype = { 6 send: function(message, to) { 7 this.chatroom.send(message, this, to); 8 }, 9 receive: function(message, from) { 10 log.add(from.name + " to " + this.name + ": " + message); 11 } 12 }; 13 var Chatroom = function() { 14 var participants = {}; 15 16 return { 17 18 register: function(participant) { 19 participants[participant.name] = participant; 20 participant.chatroom = this; 21 }, 22 23 send: function(message, from, to) { 24 if (to) { 25 to.receive(message, from); 26 } else { 27 for (key in participants) { 28 if (participants[key] !== from) { 29 participants[key].receive(message, from); 30 } 31 } 32 } 33 } 34 }; 35 }; 36 var log = (function() { 37 var log = ""; 38 39 return { 40 add: function(msg) { log += msg + "\n"; }, 41 show: function() { alert(log); log = ""; } 42 } 43 })(); 44 45 function run() { 46 var yoko = new Participant("Yoko"); 47 var john = new Participant("John"); 48 var paul = new Participant("Paul"); 49 var ringo = new Participant("Ringo"); 50 var chatroom = new Chatroom(); 51 chatroom.register(yoko); 52 chatroom.register(john); 53 chatroom.register(paul); 54 chatroom.register(ringo); 55 yoko.send("All you need is love."); 56 yoko.send("I love you John."); 57 john.send("Hey, no need to broadcast", yoko); 58 paul.send("Ha, I heard that!"); 59 ringo.send("Paul, what do you think?", paul); 60 log.show(); 61 }
在示例代碼中我們有四個參與者,加入聊天會話通過注冊一個聊天室(中介)。每個參與者的參與對象的代表。參與者相互發送消息和聊天室的處理路由。
這里的聊天室對象就起到了中介的作用,協調其他的對象,進行合理的組織,降低耦合。
二,源碼案例參考
我們應該很熟悉MVC三層模型實體模型(Model)、視圖表現層(View)還有控制層(Control/Mediator)。
控制層便是位於表現層與模型層之間的中介者。籠統地說MVC也算是中介者模式在框架設計中的一個應用。
三,案例引入
1 function Player(name) { 2 this.points = 0; 3 this.name = name; 4 } 5 Player.prototype.play = function () { 6 this.points += 1; 7 mediator.played(); 8 }; 9 var scoreboard = { 10 element:document.getElementById('results'), 11 update:function (score) { 12 var i, msg = ''; 13 for (i in score) { 14 if (score.hasOwnProperty(i)) { 15 msg += '<p><strong>' + i + '<\/strong>: '; 16 msg += score[i]; 17 msg += '<\/p>'; 18 } 19 } 20 this.element.innerHTML = msg; 21 } 22 }; 23 var mediator = { 24 players:{}, 25 setup:function () { 26 var players = this.players; 27 players.home = new Player('Home'); 28 players.guest = new Player('Guest'); 29 }, 30 played:function () { 31 var players = this.players, 32 score = { 33 Home:players.home.points, 34 Guest:players.guest.points 35 }; 36 scoreboard.update(score); 37 }, 38 keypress:function (e) { 39 e = e || window.event; 40 if (e.which === 49) { 41 mediator.players.home.play(); 42 return; 43 } 44 if (e.which === 48) { 45 mediator.players.guest.play(); 46 return; 47 } 48 } 49 }; 50 mediator.setup(); 51 window.onkeypress = mediator.keypress; 52 setTimeout(function () { 53 window.onkeypress = null; 54 console.log('Game over!'); 55 }, 30000);
四,總結一下
Why Mediator ?
各個對象之間的交互操作非常多,每個對象的行為操作都依賴彼此對方,修改一個對象的行為,同時會涉及到修改很多其他對象的行為,
如果使用Mediator模式,可以使各個對象間的耦合松散,只需關心和 Mediator的關系,使多對多的關系變成了一對多的關系,
可以降低系統的復雜性,提高可修改擴展性。
使用中介者模式的場合
1.一組定義良好的對象,現在要進行復雜的通信。
2.定制一個分布在多個類中的行為,而又不想生成太多的子類。
可以看出,中介對象主要是用來封裝行為的,行為的參與者就是那些對象,但是通過中介者,這些對象不用相互知道。(迪米特法則的具體實現)
使用中介者模式的優點:
1.降低了系統對象之間的耦合性,使得對象易於獨立的被復用。
2.提高系統的靈活性,使得系統易於擴展和維護。
使用中介者模式的缺點:
中介者模式的缺點是顯而易見的,因為這個“中介“承擔了較多的責任,所以一旦這個中介對象出現了問題,那么整個系統就會受到重大的影響。
哈哈哈,本篇結束,未完待續,希望和大家多多交流夠溝通,共同進步。。。。。。呼呼呼……(*^__^*)