作用:
可以将复杂的类进行一些拆分,让抽象和实现进行分离解耦,可以让每一个部分都可以单独维护
方便扩展和维护文章来源:https://www.toymoban.com/news/detail-835060.html
示例:
class Obj {
constructor(person) {
this.person = person
this.name = person.name
}
getHobby(){
return this.person.hobby
}
getBehavior(){
return this.person.behavior()
}
}
class Human {
constructor(hobby) {
this.name = '打工人'
this.hobby = hobby
}
behavior() {
console.log('一身打工人的怨气')
}
}
class Deity {
constructor(hobby) {
this.name = '神仙'
this.hobby = hobby
}
behavior() {
console.log('过着神仙般的生活')
}
}
const wjt = new Obj(new Human('抖音刷美女,或者打游戏'))
const sunwukong = new Obj(new Deity('定身七仙女,然后吃桃子'))
console.log(wjt.name+'的爱好:'+wjt.getHobby())
console.log(sunwukong.name+'的爱好:'+sunwukong.getHobby())
文章来源地址https://www.toymoban.com/news/detail-835060.html
到了这里,关于js设计模式:桥接模式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!