适配器模式
适配器模式「Adapter Pattern」:将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器「Wrapper」
模式动机
实现
const googleMap = {
show: function() {
console.log('开始渲染谷歌地图')
}
}
const baiduMap = {
display: function() {
console.log('开始渲染百度地图')
}
}var baiduMapAdapter = {
show: function() {
baiduMap.display()
}
}
// 这样就可以正确的渲染了
renderMap(googleMap)
renderMap(baiduMapAdapter)小结
Last updated
Was this helpful?