物件導向設計鼓勵把行為分散到各個物件中,將物件切分成更小的粒度有助於提高可重用性。但隨著細粒度物件之間的關聯急遽增加,可重用性反而又下降了。中介者模式的作用就是解除物件與物件之間的緊耦合關係——大家都只跟中介者溝通,彼此不再直接認識。
假設我們在開發手機購買頁:使用者可以選擇顏色、輸入購買數量,頁面即時回顯選擇內容,還有一顆按鈕依庫存狀態顯示「加入購物車」或「庫存不足」。
<div>
<span>請選擇顏色</span>
<select id="selColor">
<option value="roseGold">玫瑰金</option>
<option value="luxuryGold">土豪金</option>
</select>
</div>
<div>
<span>請輸入購買數量:</span>
<input type="text" id="selNum" />
</div>
<div>
<span>您選擇的顏色為:</span><strong id="conColor"></strong>
<span>您選擇的數量為:</span><strong id="conNum"></strong>
</div>
<button id="nextBtn">加入購物車</button>
// 庫存量
var goods = { roseGold: 10, luxuryGold: 10 };
var colorSelect = document.getElementById("selColor"),
numberInput = document.getElementById("selNum"),
colorInfo = document.getElementById("conColor"),
numberInfo = document.getElementById("conNum"),
nextBtn = document.getElementById("nextBtn");
colorSelect.onchange = function() {
var color = colorSelect.value, // 顏色
number = +numberInput.value, // 數量
stock = goods[color]; // 對應顏色的庫存量
colorInfo.innerHTML = color;
if(!color) {
nextBtn.disabled = true;
nextBtn.innerHTML = "請選擇手機顏色";
return;
}
if(!number || (((number - 0) | 0) !== (number - 0))) {
nextBtn.disabled = true;
nextBtn.innerHTML = "請選擇手機數量";
return;
}
if(number > stock) {
nextBtn.disabled = true;
nextBtn.innerHTML = "庫存不足";
return;
}
nextBtn.disabled = false;
nextBtn.innerHTML = "加入購物車";
};
/* 購買數量同樣要處理一次 */
問題在這裡只是還沒爆發。當產品經理說要多加一個「手機內存」下拉選單時,你必須在每個監聽函式裡重複一遍全部判斷,改動面急遽擴大,而且數量欄位的邏輯還得再複製一次。
改造後,所有節點物件只跟中介者溝通。下拉選單或輸入框發生事件時,只做一件事:通知中介者「我變了」,並把自己作為參數傳進去,讓中介者辨認是誰觸發的;其餘判斷與回顯全交給中介者。
<div>
<span>請選擇顏色:</span>
<select id="selColor">
<option value="roseGold">玫瑰金</option>
<option value="luxuryGold">土豪金</option>
</select>
</div>
<div>
<span>請選擇內存:</span>
<select id="selMemory">
<option value="16G">16G</option>
<option value="64G">64G</option>
</select>
</div>
<div>
<span>請輸入購買數量:</span>
<input type="text" id="selNum" />
</div>
<div>
<span>您選擇的顏色為:</span><strong id="conColor"></strong>
<span>您選擇的內存為:</span><strong id="conMemory"></strong>
<span>您選擇的數量為:</span><strong id="conNum"></strong>
</div>
<button id="nextBtn">加入購物車</button>
// 庫存量以「顏色|內存」為鍵
var goods = {
"roseGold|16G": 10,
"roseGold|32G": 10,
"luxuryGold|16G": 10,
"luxuryGold|32G": 10
};
var colorSelect = document.getElementById("selColor"),
memorySelect = document.getElementById("selMemory"),
numberInput = document.getElementById("selNum"),
colorInfo = document.getElementById("conColor"),
memeryInfo = document.getElementById("conMemory"),
numberInfo = document.getElementById("conNum"),
nextBtn = document.getElementById("nextBtn");
var mediator = (function() {
return {
changed: function(obj) {
var color = colorSelect.value,
memory = memorySelect.value,
number = +numberInput.value,
stock = goods[color + '|' + memory];
// 依觸發來源決定要更新哪個回顯區
if(obj === colorSelect) {
colorInfo.innerHTML = color;
} else if(obj === memorySelect) {
memeryInfo.innerHTML = memory;
} else if(obj === numberInput) {
numberInfo.innerHTML = number;
}
if(!color) {
nextBtn.disabled = true; nextBtn.innerHTML = "請選擇手機顏色"; return;
}
if(!memory) {
nextBtn.disabled = true; nextBtn.innerHTML = "請選擇手機內存"; return;
}
if(!number || (((number - 0) | 0) !== (number - 0))) {
nextBtn.disabled = true; nextBtn.innerHTML = "請選擇手機數量"; return;
}
if(number > stock) {
nextBtn.disabled = true; nextBtn.innerHTML = "庫存不足"; return;
}
nextBtn.disabled = false;
nextBtn.innerHTML = "加入購物車";
}
};
})();
// 事件函式只負責通知中介者
colorSelect.onchange = function(){ mediator.changed(this); };
memorySelect.onchange = function(){ mediator.changed(this); };
numberInput.oninput = function(){ mediator.changed(this); };
之後要再加一個「版本」下拉選單,只需要多一個節點、在中介者裡多一個 else if 分支,其他監聽函式完全不用動。
迪米特法則也叫最少知識原則,指的是一個物件應該盡可能少地了解其他物件,避免「城門失火,殃及魚池」。中介者模式正是這條原則的一種實現:節點之間不再互相依賴,改為依賴一個穩定的仲介。
最大的缺點是系統中多了一個中介物件。原本分散在各物件間的互動複雜度,被集中轉移到中介者身上,因此中介者往往會膨脹成一個巨大的、難以維護的物件。本文的 mediator 就是個小例子——如果判斷規則再漲十倍,它就會變成需要拆分的上帝物件。
判斷要不要用,可以這樣看:如果物件之間的耦合確實已導致呼叫與維護困難,而且這種複雜度隨專案演進呈指數成長,那麼用中介者模式重構是划算的;如果只是兩三個元件的簡單互動,直接寫反而更清楚。