Go設(shè)計(jì)模式之單例模式講解和代碼示例
Go 單例模式講解和代碼示例.
單例是一種創(chuàng)建型設(shè)計(jì)模式, 讓你能夠保證一個類只有一個實(shí)例, 并提供一個訪問該實(shí)例的全局節(jié)點(diǎn)。
單例擁有與全局變量相同的優(yōu)缺點(diǎn)。 盡管它們非常有用, 但卻會破壞代碼的模塊化特性。
在某些其他上下文中, 你不能使用依賴于單例的類。 你也將必須使用單例類。 絕大多數(shù)情況下, 該限制會在創(chuàng)建單元測試時出現(xiàn)。
概念示例
通常而言, 單例實(shí)例會在結(jié)構(gòu)體首次初始化時創(chuàng)建。 為了實(shí)現(xiàn)這一操作, 我們在結(jié)構(gòu)體中定義一個 getInstance
獲取實(shí)例方法。 該方法將負(fù)責(zé)創(chuàng)建和返回單例實(shí)例。 創(chuàng)建后, 每次調(diào)用 getInstance
時都會返回相同的單例實(shí)例。
協(xié)程方面又有什么需要注意的嗎? 每當(dāng)多個協(xié)程想要訪問實(shí)例時, 單例結(jié)構(gòu)體就必須返回相同的實(shí)例。 正因如此, 單例設(shè)計(jì)模式的實(shí)施工作很容易出錯。 下方的例子表示了創(chuàng)建單例的正確方式。
一些值得注意的地方:
- 最開始時會有
nil
檢查, 確保singleInstance
單例實(shí)例在最開始時為空。 這是為了防止在每次調(diào)用getInstance
方法時都去執(zhí)行消耗巨大的鎖定操作。 如果檢查不通過, 則就意味著singleInstance
字段已被填充。 singleInstance
結(jié)構(gòu)體將在鎖定期間創(chuàng)建。- 在獲取到鎖后還會有另一個
nil
檢查。 這是為了確保即便是有多個協(xié)程繞過了第一次檢查, 也只能有一個可以創(chuàng)建單例實(shí)例。 否則, 所有協(xié)程都會創(chuàng)建自己的單例結(jié)構(gòu)體實(shí)例。
single.go: 單例
package main import ( "fmt" "sync" ) var lock = &sync.Mutex{} type single struct { } var singleInstance *single func getInstance() *single { if singleInstance == nil { lock.Lock() defer lock.Unlock() if singleInstance == nil { fmt.Println("Creating single instance now.") singleInstance = &single{} } else { fmt.Println("Single instance already created.") } } else { fmt.Println("Single instance already created.") } return singleInstance }
main.go: 客戶端代碼
package main import ( "fmt" ) func main() { for i := 0; i < 30; i++ { go getInstance() } // Scanln is similar to Scan, but stops scanning at a newline and // after the final item there must be a newline or EOF. fmt.Scanln() }
output.txt: 執(zhí)行結(jié)果
Creating single instance now.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
Single instance already created.
另一個例子
init
函數(shù)
我們可以在 init
函數(shù)中創(chuàng)建單例實(shí)例。 這僅適用于實(shí)例的早期初始化工作已經(jīng)確定時。 init
函數(shù)僅會在包中的每個文件里調(diào)用一次, 所以我們可以確定其只會創(chuàng)建一個實(shí)例。
sync.Once
sync.Once
僅會執(zhí)行一次操作。 可查看下面的代碼:
syncOnce.go: 單例
package main import ( "fmt" "sync" ) var once sync.Once type single struct { } var singleInstance *single func getInstance() *single { if singleInstance == nil { once.Do( func() { fmt.Println("Creating single instance now.") singleInstance = &single{} }) } else { fmt.Println("Single instance already created.") } return singleInstance }
main.go: 客戶端代碼
package main import ( "fmt" ) func main() { for i := 0; i < 30; i++ { go getInstance() } // Scanln is similar to Scan, but stops scanning at a newline and // after the final item there must be a newline or EOF. fmt.Scanln() }
output.txt: 執(zhí)行結(jié)果
Creating single instance now.
Single instance already created.
Single instance already created.
到此這篇關(guān)于Go設(shè)計(jì)模式之單例模式講解和代碼示例的文章就介紹到這了,更多相關(guān)Go單例模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言利用ffmpeg轉(zhuǎn)hls實(shí)現(xiàn)簡單視頻直播
這篇文章主要為大家介紹了Go語言利用ffmpeg轉(zhuǎn)hls實(shí)現(xiàn)簡單視頻直播,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04golang數(shù)組和切片作為參數(shù)和返回值的實(shí)現(xiàn)
本文主要介紹了golang數(shù)組和切片作為參數(shù)和返回值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Go語言使用defer+recover解決panic導(dǎo)致程序崩潰的問題
如果協(xié)程出現(xiàn)了panic,就會造成程序的崩潰,這時可以在goroutine中使用recover來捕獲panic,進(jìn)行處理,本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-09-09golang實(shí)現(xiàn)微信小程序商城后臺系統(tǒng)(moshopserver)
這篇文章主要介紹了golang實(shí)現(xiàn)微信小程序商城后臺系統(tǒng)(moshopserver),本文通過截圖實(shí)例代碼的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02Go語言使用MongoDB數(shù)據(jù)庫詳細(xì)步驟
mongodb是一種高性能、開源、文檔型的nosql數(shù)據(jù)庫,被廣泛應(yīng)用于web應(yīng)用、大數(shù)據(jù)以及云計(jì)算領(lǐng)域,下面這篇文章主要給大家介紹了關(guān)于Go語言使用MongoDB數(shù)據(jù)庫的詳細(xì)步驟,需要的朋友可以參考下2024-05-05GO接收GET/POST參數(shù)及發(fā)送GET/POST請求的實(shí)例詳解
這篇文章主要介紹了GO接收GET/POST參數(shù)及發(fā)送GET/POST請求,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12