前端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()
本文暂时没有评论,来添加一个吧(●'◡'●)