橋接模式
橋接模式是指将抽象部分與它的實作部分分離,使它們各自獨立的變化,通過使用組合關系代替繼承關系,降低抽象和實作兩個可變次元的耦合度。
// 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();