适配器模式 ⭐
定义
适配器模式(Adapter Pattern),是作为两个不兼容的接口之间的桥梁。
插头转换器 就是生活中的适配器。
实现
js
const googleMap = {
show: function () {
console.log('开始渲染谷歌地图')
},
}
const baiduMap = {
display: function () {
console.log('开始渲染百度地图')
},
}
const baiduMapAdapter = {
show: function () {
return baiduMap.display()
},
}
const renderMap = function (map) {
if (map.show instanceof Function) {
map.show()
}
}
renderMap(googleMap)
renderMap(baiduMapAdapter)