设计模式-桥接模式
概念
- 用于把抽象化与实现化解耦
- 使得二者可以独立变化
演示
class ColorShape {yellowCircle() {console.log('yellow circle')}redCircle() {console.log('red circle')}yellowTriangle() {console.log('yellow triangle')}redTriangle() {console.log('red triangle')}
}// 测试
let cs = new ColorShape()
cs.yellowCircle()
cs.redCircle()
cs.yellowTriangle()
cs.redTriangle()
下面是桥接模式代码:
class Color {constructor(name) {this.name = name}
}class Shape {constructor(name, color) {this.name = namethis.color = color}draw() {console.log(`${this.color.name} ${this.name}`)}
}// 测试
let red = new Color('red')
let yellow = new Color('yellow')let circle = new Shape('circle', red)
circle.draw()
let triangle = new Shape('triangle', yellow)
triangle.draw()
设计原则验证
- 抽象和实现分离,解耦
- 符合开放封闭原则