Go設(shè)計(jì)模式之中介者模式講解和代碼示例
Go 中介者模式講解和代碼示例
中介者能使得程序更易于修改和擴(kuò)展, 而且能更方便地對(duì)獨(dú)立的組件進(jìn)行復(fù)用, 因?yàn)樗鼈儾辉僖蕾囉诤芏嗥渌念悺?/p>
概念示例
中介者模式的一個(gè)絕佳例子就是火車站交通系統(tǒng)。 兩列火車互相之間從來(lái)不會(huì)就站臺(tái)的空閑狀態(tài)進(jìn)行通信。 stationManager
車站經(jīng)理可充當(dāng)中介者, 讓平臺(tái)僅可由一列入場(chǎng)火車使用, 而將其他火車放入隊(duì)列中等待。 離場(chǎng)火車會(huì)向車站發(fā)送通知, 便于隊(duì)列中的下一列火車進(jìn)站。
train.go: 組件
package main type Train interface { arrive() depart() permitArrival() }
passengerTrain.go: 具體組件
package main import "fmt" type PassengerTrain struct { mediator Mediator } // 火車停靠 func (pt *PassengerTrain) arrive() { if !pt.mediator.canArrive(pt) { fmt.Println("PassengerTrain: Arrival blocked, waiting") return } fmt.Println("PassengerTrain: arrived") } // 獲取離開(kāi) func (pt *PassengerTrain) depart() { fmt.Println("PassengerTrain: leaving") pt.mediator.notifyAboutDeparture() } func (pt *PassengerTrain) permitArrival() { fmt.Println("PassengerTrain: Arrival permitted, arriving") pt.arrive() }
freightTrain.go: 具體組件
package main import "fmt" type FreightTrain struct { mediator Mediator } func (g *FreightTrain) arrive() { if !g.mediator.canArrive(g) { fmt.Println("FreightTrain: Arrival blocked, waiting") return } fmt.Println("FreightTrain: arrived") } func (g *FreightTrain) depart() { fmt.Println("FreightTrain: leaving") g.mediator.notifyAboutDeparture() } func (g *FreightTrain) permitArrival() { fmt.Println("FreightTrain: Arrival permitted") g.arrive() }
mediator.go: 中介者接口
package main type Mediator interface { canArrive(Train) bool notifyAboutDeparture() }
stationManager.go: 具體中介者
package main type StationManager struct { isPlatformFree bool trainQueue []Train } func newStationManager() *StationManager { return &StationManager{ isPlatformFree: true, } } func (s *StationManager) canArrive(t Train) bool { if s.isPlatformFree { s.isPlatformFree = false return true } s.trainQueue = append(s.trainQueue, t) return false } func (s *StationManager) notifyAboutDeparture() { if !s.isPlatformFree { s.isPlatformFree = true } if len(s.trainQueue) > 0 { firstTrainInQueue := s.trainQueue[0] s.trainQueue = s.trainQueue[1:] firstTrainInQueue.permitArrival() } }
main.go: 客戶端代碼
package main func main() { stationManager := newStationManager() passengerTrain := &PassengerTrain{ mediator: stationManager, } freightTrain := &FreightTrain{ mediator: stationManager, } passengerTrain.arrive() freightTrain.arrive() passengerTrain.depart() }
到此這篇關(guān)于Go設(shè)計(jì)模式之中介者模式講解和代碼示例的文章就介紹到這了,更多相關(guān)Go 中介者模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言Goroutines?泄漏場(chǎng)景與防治解決分析
這篇文章主要為大家介紹了Go語(yǔ)言Goroutines?泄漏場(chǎng)景與防治解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12golang?使用sort.slice包實(shí)現(xiàn)對(duì)象list排序
這篇文章主要介紹了golang?使用sort.slice包實(shí)現(xiàn)對(duì)象list排序,對(duì)比sort跟slice兩種排序的使用方式區(qū)別展開(kāi)內(nèi)容,需要的小伙伴可以參考一下2022-03-03Golang官方限流器庫(kù)實(shí)現(xiàn)限流示例詳解
這篇文章主要為大家介紹了Golang官方限流器庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08goland中使用leetcode插件實(shí)現(xiàn)
本文主要介紹了goland中使用leetcode插件實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04golang gin 框架 異步同步 goroutine 并發(fā)操作
這篇文章主要介紹了golang gin 框架 異步同步 goroutine 并發(fā)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12Golang處理parquet文件實(shí)戰(zhàn)指南
這篇文章主要給大家介紹了關(guān)于Golang處理parquet文件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Golang具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03