設(shè)計(jì)模式j(luò)s篇--橋接模式

一、定義

通過(guò)提供抽象化和現(xiàn)實(shí)化之間的橋接結(jié)構(gòu),實(shí)現(xiàn)二者的解耦。
2個(gè)角色:
(1)擴(kuò)充抽象類(lèi)
(2)具體實(shí)現(xiàn)類(lèi)

二、舉例

eg1: github上面原有的例子(https://github.com/sohamkamani/javascript-design-patterns-for-humans#-bridge)。

// 場(chǎng)景:更換主題
// About Careers是兩個(gè)抽象類(lèi),都有自己的主題成員
class About{ 
    constructor(theme) {
        this.theme = theme
    }    
    getContent() {
        return "About page in " + this.theme.getColor()
    }
}
class Careers{
   constructor(theme) {
       this.theme = theme
   } 
   getContent() {
       return "Careers page in " + this.theme.getColor()
   } 
}
// DarkTheme LightTheme AquaTheme是具體實(shí)現(xiàn)類(lèi),不同的主題有不同的效果
class DarkTheme{
    getColor() {
        return 'Dark Black'
    }
}
class LightTheme{
    getColor() {
        return 'Off white'
    }
}
class AquaTheme{
    getColor() {
        return 'Light blue'
    }
}
// 調(diào)用
const darkTheme = new DarkTheme()
const about = new About(darkTheme)
const careers = new Careers(darkTheme)
console.log(about.getContent() )// "About page in Dark Black"
console.log(careers.getContent() )// "Careers page in Dark Black"

eg2:事件監(jiān)聽(tīng)

/*
 點(diǎn)擊事件獲取數(shù)據(jù)
 弊端:該函數(shù)直接使用this.id,只能工作在瀏覽器中,如果要對(duì)這個(gè)API函數(shù)做單元測(cè)試,或在命令行環(huán)境中執(zhí)行它,是非常不方便的
*/
getData () {
   api.fetchCompanyWalletApi({id: this.id}).then(data => {
        // do sth
   })
}
/*
1、將id作為參數(shù)傳遞
2、用橋接模式把抽象隔離開(kāi)來(lái)
好處:拓寬了適用范圍,因?yàn)楝F(xiàn)在getData 并沒(méi)有和事件對(duì)象捆綁在一起,只需要提供一個(gè)ID就可以在單元測(cè)試中運(yùn)行這個(gè)API。此外,也可以在命令行環(huán)境中對(duì)這個(gè)接口進(jìn)行快速測(cè)試。
*/
getData (id) {
   api.fetchCompanyWalletApi({id: id}).then(data => {
       // do sth
   })
}
// 綁定事件
function getDataBridge(){
      getData (this.id);
}

eg3:連接公開(kāi)的API代碼和私用實(shí)現(xiàn)的代碼

let Student = function () {
  // 私有變量
  let name = 'aa';
  // getName 訪問(wèn)私用成員變量 name(特權(quán)函數(shù))
  this.getName = function () {
    return name;
  }
  // 私有方法
  let getAge = function ()  {
    // do sth
    return 1;
  }
// getAgeBridge 訪問(wèn)私用方法 name(特權(quán)函數(shù))
  this.getAgeBridge = function () {
      return getAge ()
  }
}
let student = new Student()
student.getName()
student.getAgeBridge()

eg3:橋接模式聯(lián)合幾個(gè)類(lèi)

const Fn1 = function(a) {
  // dosomething...  
}
const Fn2 = function(b) {
  // dosomething...
}
const Bridge = function(a, b){
  this.one = new Fn1(a)
  this.two = new Fn2(b)
}
三、總結(jié)

優(yōu)點(diǎn):
分離接口和實(shí)現(xiàn)部分
提供可擴(kuò)展性
實(shí)現(xiàn)細(xì)節(jié)對(duì)客戶透明,可以對(duì)客戶隱藏實(shí)現(xiàn)細(xì)節(jié)

缺點(diǎn):
大量的類(lèi)將導(dǎo)致開(kāi)發(fā)成本增加,同時(shí)在性能方面可能也會(huì)有所降低

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,664評(píng)論 4 61
  • 在偶像劇里談現(xiàn)實(shí),這好像有點(diǎn)不太現(xiàn)實(shí)。你可以高仿經(jīng)典也可以玩瑪麗蘇,但是主角不鮮還辣眼,內(nèi)容不甜還作妖,那就很對(duì)不...
    特立獨(dú)行的MissK閱讀 4,030評(píng)論 0 2
  • 我不太愛(ài)說(shuō)話,與人在一起交談,一般都是別人說(shuō)我聽(tīng)。時(shí)間一長(zhǎng),發(fā)現(xiàn)聽(tīng)別人說(shuō)也是一件蠻有趣的事情。 作為一個(gè)聽(tīng)者,最喜...
    彼岸很美閱讀 4,156評(píng)論 3 9
  • 嗨,朋友們?cè)绨玻∮质怯幸恢艿拈_(kāi)始。上周你的任務(wù)清單完成的怎樣呢?上一周都干了啥? 1.靜靜沒(méi)有夜生活 基本上都...
    溯本源閱讀 786評(píng)論 0 0
  • 一直渴求一個(gè)突破口,沖出隱形的避障,卻難以逃脫被裹挾著進(jìn)入洪流的命運(yùn),昏昏沉沉的嘈雜中,不知未來(lái)何方 身邊的事物不...
    衡七_(dá)2閱讀 138評(píng)論 0 0

友情鏈接更多精彩內(nèi)容