前端JavaScript设计模式-适配器模式:
适配器模式是为了解决两个已有接口不匹配的问题,不需要更改某个接口,经过一系列转换,就能实现他们之间的协同工作
应用场景就外币兑换举例来说,在国外人民币不适用,需要进行汇率兑换美元才能使用
// 适配器
class Adaptee{
specificRequest(){
return '美元'
}
}
// 转换器
class Target{
constructor(){
this.adaptee = new Adaptee()
}
request(){
let info = this.adaptee.specificRequest()
return `人民币 -- 转换 -- ${info}`
}
}
let target = new Target()
let res = target.Request()
console.log(res)
本文暂时没有评论,来添加一个吧(●'◡'●)