作用:
可以將復(fù)雜的類進(jìn)行一些拆分,讓抽象和實(shí)現(xiàn)進(jìn)行分離解耦,可以讓每一個(gè)部分都可以單獨(dú)維護(hù)
方便擴(kuò)展和維護(hù)文章來源:http://www.zghlxwxcb.cn/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())
文章來源地址http://www.zghlxwxcb.cn/news/detail-835060.html
到了這里,關(guān)于js設(shè)計(jì)模式:橋接模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!