天天看点

JavaScript-桥接模式桥接模式

桥接模式

桥接模式是指将抽象部分与它的实现部分分离,使它们各自独立的变化,通过使用组合关系代替继承关系,降低抽象和实现两个可变维度的耦合度。
// Phone中,Brand品牌与Mode型号是两个独立互不影响的维度
class Phone {
  constructor(brand, mode) {
    this.brand = brand;
    this.mode = mode;
  }
  showPhone() {
    console.log(
      `手机品牌: ${this.brand.getBrand()}, 型号: ${this.mode.getMode()}`
    );
  }
}


class Brand {
  constructor(name) {
    this.name = name;
  }
  getBrand() {
    return this.name;
  }
}

class Mode {
  constructor(name) {
    this.name = name;
  }
  getMode() {
    return this.name;
  }
}

const huawei = new Phone(new Brand("华为"), new Mode("Mate 40"));
huawei.showPhone();

           

继续阅读