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

网站首页 > 技术文章 正文

高级前端必会设计模式之桥接模式

ins518 2024-09-11 09:28:13 技术文章 31 ℃ 0 评论

前端JavaScript设计模式-桥接模式:

桥接模式用于把抽象化与实现化解耦,使二者可以独立变化,可以根据不同业务场景进行使用

可重复多次使用,常用于项目中方法函数的封装

// 着色器
class Color{
  constructor(name){
    this.name = name
  }
}

// 形状模具
class Shape{
  constructor(name,color){
    this.name = name
    this.color = color
  }
  draw(){
    console.log(`颜色${this.color.name},形状位${this.name}`)
  }
}

// 红色圆形
let red = new Color('red')
let circular = new Shape('circular',red)
circular.draw()

// 黄色方形
let yellow = new Color('yellow')
let square = new Shape('square',yellow)
square.draw()

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

欢迎 发表评论:

最近发表
标签列表