2020-06-17 框架設(shè)計(jì)

  1. 請(qǐng)描述什么是依賴(lài)倒置原則,為什么有時(shí)候依賴(lài)倒置原則又被稱(chēng)為好萊塢原則憾股?
    依賴(lài)倒置原則是一種解耦模塊間關(guān)系的方法鹿蜀,它要求上層模塊不能依賴(lài)于底層模塊,他們應(yīng)該共同依賴(lài)于一個(gè)抽象服球;抽象不能依賴(lài)于實(shí)現(xiàn)茴恰,實(shí)現(xiàn)應(yīng)該依賴(lài)于抽象。
    在底層實(shí)現(xiàn)發(fā)生變化或者引入新的底層實(shí)現(xiàn)時(shí)斩熊,通過(guò)共同依賴(lài)于抽象往枣,可以使得改動(dòng)對(duì)上層模塊的影響最小化。
    好萊塢原則通俗講是你不要調(diào)用我粉渠,讓我來(lái)調(diào)用你分冈。這個(gè)關(guān)系的反轉(zhuǎn)和依賴(lài)倒置的核心思想是一致的,好萊塢所講的可以理解為框架會(huì)定義一系列的接口霸株,各種基于框架開(kāi)發(fā)的應(yīng)用程序只需要實(shí)現(xiàn)這些接口雕沉,框架在啟動(dòng)之后它會(huì)來(lái)調(diào)用應(yīng)用程序?qū)崿F(xiàn)的這些接口,讓程序運(yùn)行起來(lái)去件。

  2. 請(qǐng)描述一個(gè)你熟悉的框架坡椒,是如何實(shí)現(xiàn)依賴(lài)倒置原則的饺著。
    最近在做一個(gè)對(duì)圖片做離線(xiàn)處理的pipeline系統(tǒng),主要基于https://github.com/digitalocean/firebolt這個(gè)框架進(jìn)行的開(kāi)發(fā)肠牲。
    此框架定義了consumer中接收消息的source node的接口,以及pipeline處理中算子node的接口靴跛,作為使用框架的開(kāi)發(fā)人員只需要按接口要求把處理邏輯封裝在這些接口方法中缀雳,然后在程序啟動(dòng)前,將實(shí)現(xiàn)注冊(cè)到firebolt框架梢睛,然后啟動(dòng)firebolt肥印,框架就會(huì)按照開(kāi)發(fā)者定義的處理流程配置文件來(lái)按序執(zhí)行pipeline處理。

source node負(fù)責(zé)接收消息

type Source interface {
    Setup(config map[string]string, recordsch chan []byte) error
    Start() error
    Shutdown() error
    Receive(msg fbcontext.Message) error
}

sync node 負(fù)責(zé)處理業(yè)務(wù)邏輯的算子

type SyncNode interface {
    Setup(config map[string]string) error
    Process(event *firebolt.Event) (*firebolt.Event, error)
    Shutdown() error
    Receive(msg fbcontext.Message) error
}

config file 用來(lái)定義這個(gè)pipeline處理流程

source:                                 # one and only one source is required
  name: kafkaconsumer
  params:
    brokers: ${KAFKA_BROKERS}           # environment variables are supported
    consumergroup: testapp
    topic: logs-all
    buffersize: 1000                    # sources do not normally need buffering; this value is a pass-thru to the underlying kafka consumer
nodes:
  - name: firstnode
    workers: 1                          # each node can be configured to run any number of workers (goroutines), the default is 1
    buffersize: 100                     # each node has a buffered input channel for the data that is ready to be processed, default size is 1
    params:                             # params are passed as a map to the node's Setup() during initialization
      param1.1: value1.1
      param1.2: value1.2
    children:                           # a node may have many children, the events returned by the node are passed to all child node's input channels
      - name: secondnode
        error_handler:                  # errors returned by 'secondnode' will be passed to this error handler
          name: errorkafkaproducer      # we provide built-in 'errorkafkaproducer' that writes JSON error reports to a Kafka topic
          buffersize: 100
          discard_on_full_buffer: true  # if the buffer is full discard messages to avoid sending backpressure downstream for a low priority function
        children:
          - name: thirdnode
            id: third-node-id           # you can use the same node type in your hierarchy twice, but its id (defaults to name) must be unique
            workers: 3
            buffersize: 300
            params:
              param3.1: value3.1
              param3.2: value3.2

主程序通過(guò)node.GetRegistry().RegisterNodeType注冊(cè)已實(shí)現(xiàn)的node绝葡,并啟動(dòng)executor

// first register any firebolt source or node types that are not built-in
    node.GetRegistry().RegisterNodeType("jsonconverter", func() node.Node {
            return &jsonconverter.JsonConverter{}
        }, reflect.TypeOf(([]byte)(nil)), reflect.TypeOf(""))
    
    // start the executor running - it will build the source and nodes that process the stream
    ex, err := executor.New(configFile)
    if err != nil {
        fmt.Printf("failed to initialize firebolt for config file %s: %v\n", configFile, err)
        os.Exit(1)
    }
    ex.Execute() // the call to Execute will block while the app runs

由于golang沒(méi)有Java強(qiáng)大的泛型和annotation深碱,因此需要在主程序中顯示的注冊(cè)各種實(shí)現(xiàn)好的node

  1. 請(qǐng)用接口隔離原則優(yōu)化 Cache 類(lèi)的設(shè)計(jì),畫(huà)出優(yōu)化后的類(lèi)圖
type CacheConfig interface {
}

type CacheStorage interface {
    Get(key string) (interface{}, error)
    Set(key, value string) error
    Delete(key string) error
}

type CacheHandler interface {
    ReBuild(conf CacheConfig) (CacheStorage, error)
}

type CacheProxy struct {
    ActiveCache CacheStorage
    CacheHandler
}

應(yīng)用程序中使用時(shí)藏畅,使用方法為

var (
  err error
  activeCache CacheStorage
)
activeCache, err = NewCacheProxy(cacheConf)

遠(yuǎn)程系統(tǒng)調(diào)用時(shí)敷硅,使用方法為

var (
  err error
  var cacheHandler CacheHandler
)
cacheHandler, err = NewCacheProxy(cacheConf)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市愉阎,隨后出現(xiàn)的幾起案子绞蹦,更是在濱河造成了極大的恐慌,老刑警劉巖榜旦,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件幽七,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡溅呢,警方通過(guò)查閱死者的電腦和手機(jī)澡屡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)咐旧,“玉大人驶鹉,你說(shuō)我怎么就攤上這事⌒菖迹” “怎么了梁厉?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)踏兜。 經(jīng)常有香客問(wèn)我词顾,道長(zhǎng),這世上最難降的妖魔是什么碱妆? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任肉盹,我火速辦了婚禮,結(jié)果婚禮上疹尾,老公的妹妹穿的比我還像新娘上忍。我一直安慰自己骤肛,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布窍蓝。 她就那樣靜靜地躺著腋颠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吓笙。 梳的紋絲不亂的頭發(fā)上淑玫,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音面睛,去河邊找鬼絮蒿。 笑死,一個(gè)胖子當(dāng)著我的面吹牛叁鉴,可吹牛的內(nèi)容都是我干的土涝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼幌墓,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼但壮!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起常侣,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤茵肃,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后袭祟,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體验残,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年巾乳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了您没。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡胆绊,死狀恐怖氨鹏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情压状,我是刑警寧澤仆抵,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站种冬,受9級(jí)特大地震影響镣丑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜娱两,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一莺匠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧十兢,春花似錦趣竣、人聲如沸摇庙。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)卫袒。三九已至,卻和暖如春单匣,著一層夾襖步出監(jiān)牢的瞬間玛臂,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工封孙, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人讽营。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓虎忌,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親橱鹏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子膜蠢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355