外观
中介者模式
中介者模式减少多个对象间的直接通信,以中介者和对象间一对多的关系取代对象间多对多的网状关系,使各对象得以解耦。
对象间不直接通信,而是同过中介者转发,复杂交互关系转移到中介者内部管理,每个对象只需关注自身行为,表单元素、聊天室等适用中介者模式。
中介者模式符合最少知识原则,但也有缺点,中介者会占用内存,且逻辑太多时中介者可能会成为一个难以维护的巨大对象。
表单校验
以购买商品表单为例。
表单元素
html
<span>请选择颜色:</span>
<select id="colorSelect">
<option value="">请选择</option>
<option value="red">红色</option>
<option value="blue">蓝色</option>
</select>
<span>输入购买数量:</span>
<input id="numberInput" type="text"/>
<br>
<span>您选择了颜色:</span>
<div id="colorInfo"></div>
<span>您输入了数量:</span>
<div id="numberInfo"></div>
<button disabled id="nextBtn">请选择手机颜色和购买数量</button>常规写法
寻常做法是给颜色下拉框注册 change 事件,在事件中校验颜色和购买数,并在展示区显示,然后修改按钮状态和文字,接着在输入框的 input 事件中也要重复以上内容,可以看到各个对象耦合在一起,如果再增加下拉框和输入框,则联系更加复杂。
js
const goods = {
red: 3,
blue: 6
}
const colorSelect = document.getElementById('colorSelect')
const colorInfo = document.getElementById('colorInfo')
const numberInput = document.getElementById('numberInput')
const numberInfo = document.getElementById('numberInfo')
const nextBtn = document.getElementById('nextBtn')
colorSelect.onchange = function () {
const color = this.value
const number = numberInput.value
const stock = goods[color]
colorInfo.innerHTML = color
if (!color) {
nextBtn.disabled = true
nextBtn.innerHTML = '请选择手机颜色'
return
}
if (!number) {
nextBtn.disabled = true
nextBtn.innerHTML = '请输入购买数量'
return
}
if (number > stock) {
nextBtn.disabled = true
nextBtn.innerHTML = '库存不足'
return
}
nextBtn.disabled = false
nextBtn.innerHTML = '放入购物车'
}
numberInput.oninput = function () {
const color = colorSelect.value
const number = this.value
const stock = goods[color]
numberInfo.innerHTML = number
if (!color) {
nextBtn.disabled = true
nextBtn.innerHTML = '请选择手机颜色'
return
}
if (!number) {
nextBtn.disabled = true
nextBtn.innerHTML = '请输入购买数量'
return
}
if (number > stock) {
nextBtn.disabled = true
nextBtn.innerHTML = '库存不足'
return
}
nextBtn.disabled = false
nextBtn.innerHTML = '放入购物车'
}使用中介者模式
此时可引入中介者,所有节点只与中介者通信,change 或 input 事件触发时仅仅通知中介者,同时把自身作为参数传入,以便中介者辨别是谁发生了改变,剩下的事交给中介者来处理。
js
const goods = {
red: 3,
blue: 6
}
const mediator = (function () {
const colorSelect = document.getElementById('colorSelect')
const colorInfo = document.getElementById('colorInfo')
const numberInput = document.getElementById('numberInput')
const numberInfo = document.getElementById('numberInfo')
const nextBtn = document.getElementById('nextBtn')
return {
change(obj) {
const color = colorSelect.value
const number = numberInput.value
const stock = goods[color]
if (obj === colorSelect) {
colorInfo.innerHTML = color
} else if (obj === numberInput) {
numberInfo.innerHTML = number
}
if (!color) {
nextBtn.disabled = true
nextBtn.innerHTML = '请选择手机颜色'
return
}
if (!number) {
nextBtn.disabled = true
nextBtn.innerHTML = '请输入购买数量'
return
}
if (number > stock) {
nextBtn.disabled = true
nextBtn.innerHTML = '库存不足'
return
}
nextBtn.disabled = false
nextBtn.innerHTML = '加入购物车'
}
}
}())
colorSelect.onchange = function () {
mediator.change(this)
}
numberInput.oninput = function () {
mediator.change(this)
}