专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

高级前端必会设计模式之适配器模式

ins518 2024-09-11 09:27:55 技术文章 49 ℃ 0 评论

前端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)

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表