1.設(shè)計(jì)模式是什么?
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為渝中企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營(yíng)銷網(wǎng)站建設(shè),渝中網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
設(shè)計(jì)模式其實(shí)就是前人總結(jié),代表了最佳實(shí)踐,對(duì)于軟件開發(fā)過程中對(duì)象的封裝模式,也是各種復(fù)雜問題,極好解耦性的解決方案。
-------------------------------------------------------------------------------------------
下面我們來說一下單例模式的基本概念和代碼:
單例類保證了全局只有唯一一個(gè)實(shí)例對(duì)象
單例提供獲取這個(gè)唯一實(shí)例的接口
其實(shí)就是保證一個(gè)類中出現(xiàn)對(duì)象的全局唯一性。
首先對(duì)于單例模式而言,有2種
懶漢模式:
#include<bits/stdc++.h> #include<mutex> using namespace std; class singleton { public: static singleton* GetInstance() { //使用雙重檢查,保證獲取鎖的資源不浪費(fèi) if(_instance == NULL) { std::lock_guard<std::mutex>lck(_mtx); if(instance == NULL) { //一下解釋標(biāo)記為a singleton *tmp = new singleton(); MemoryBarrier();//內(nèi)存柵欄。后面會(huì)進(jìn)行解釋。 _instance = tmp; } } return _instance; } static void DelInstance() { if(_instance != NULL) { delete _instance; _instance = NULL; } } private: singleton():data(0){}; singleton(const singleton&){}; singleton& operator = (const sigleton&); static singleton* _instance; static mutex _mtx; int data; } singleton* singleton::_instance = NULL; mutex singleton::_mtx;
其實(shí)單例模式?jīng)]有真想象的這么簡(jiǎn)單,百度上許多說單例模式的文章都說的有些許遺漏
這里主要解釋一下內(nèi)存柵欄的概念
如果我們將a處代碼轉(zhuǎn)換為_instance = new singleton();
這在編譯器中處理為3個(gè)部分,1.分配空間,2調(diào)用構(gòu)造函數(shù),3.賦值、
但是在某些情況話,編譯器可能進(jìn)行優(yōu)化進(jìn)行重排,然后順序變成了1,3,2.將賦值提到了構(gòu)造函數(shù)之前。
然后設(shè)想在高并發(fā)場(chǎng)景中,_instance已經(jīng)進(jìn)行了賦值,但是沒有調(diào)用構(gòu)造函數(shù),其他現(xiàn)場(chǎng)進(jìn)入,直接返回_instance。一個(gè)沒有調(diào)用構(gòu)造函數(shù)的_instance,這就會(huì)出現(xiàn)錯(cuò)誤
所以我們聲明一個(gè)臨時(shí)變量,然后添加一個(gè)內(nèi)存柵欄,其實(shí)就是指令順序的隔斷,不可提前。保證賦值構(gòu)造的完全調(diào)用,其實(shí)就有點(diǎn)類似Linux中的sigsuspend();
以上就是完善的懶漢模式。
-------------------------------------------------------------------------------------------
餓漢模式:
餓漢模式使用RAII
//1 class singleton { public: static singleton* GetInstance() { static single sInstance; return &sInstance; } private: singleton():data(0){}; singleton(const singleton&){}; singleton& operator = (const sigleton&); static singleton* _instance; int data; } //2 class singleton { public: static sington* GetInstance() { assert(_instance); return _instance; } private: singleton():data(0){}; singleton(const singleton&){}; singleton& operator = (const sigleton&); static singleton* _instance; int data; } singleton* singleton::_instance = new singleton();
以上。
網(wǎng)頁(yè)名稱:設(shè)計(jì)模式之單例模式:singleton
當(dāng)前網(wǎng)址:http://bm7419.com/article8/gegoip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站策劃、品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)