十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
什么是設(shè)計模式

設(shè)計模式代表了最佳實踐,是軟件開發(fā)過程中面臨一般問題的解決方案。
設(shè)計模式是一套被反復(fù)使用、經(jīng)過分類、代碼設(shè)計總結(jié)的經(jīng)驗。
單例模式
單例模式也叫單件模式。Singleton是一個非常常用的設(shè)計模式,幾乎所有稍微大一些的程序都會使用到它,所以構(gòu)建一個線程安全并且高效的Singleton很重要。
1. 單例類保證全局只有一個唯一實例對象。
2. 單例類提供獲取這個唯一實例的接口。
怎樣設(shè)計一個單例模式
實現(xiàn)一(不考慮線程安全)
class Singleton
{
public:
// 獲取唯一對象實例的接口函數(shù)
static Singleton* GetInstance()
{
if(_sInstance == NUL)
{
if (_sInstance == NULL)
{
_sInstance = new Singleton();
}
}
return _sInstance;
}
// 刪除實例對象
static void DelInstance()
{
if (_sInstance)
{
delete _sInstance;
_sInstance = NULL;
}
}
void Print()
{
cout<<_data<Print();
Singleton::DelInstance();
} 實現(xiàn)二
線程安全的單例 -- (懶漢模式-- lazy loading)
ps: 下面部分的加鎖使用了C++11庫的互斥鎖
class Singleton
{
public:
// 獲取唯一對象實例的接口函數(shù)
static Singleton* GetInstance()
{
// 使用雙重檢查,提高效率,避免高并發(fā)場景下每次獲取實例對象都進(jìn)行加鎖
if (_sInstance == NULL)
{
std::lock_guard lck(_mtx);
if (_sInstance == NULL)
{
// tmp = new Singleton()分為以下三個部分
// 1.分配空間 2.調(diào)用構(gòu)造函數(shù) 3.賦值
// 編譯器編譯優(yōu)化可能會把2和3進(jìn)行指令重排,這樣可能會導(dǎo)致
// 高并發(fā)場景下,其他線程獲取到未調(diào)用構(gòu)造函數(shù)初始化的對象
// 以下加入內(nèi)存柵欄進(jìn)行處理,防止編譯器重排柵欄后面的賦值
// 到內(nèi)存柵欄之前
Singleton* tmp = new Singleton();
MemoryBarrier();
_sInstance = tmp;
}
}
return _sInstance;
}
// 刪除實例對象
static void DelInstance()
{
std::lock_guard lck(_mtx);
if (_sInstance)
{
delete _sInstance;
_sInstance = NULL;
}
}
void Print()
{
cout<<_data<Print();
Singleton::DelInstance();
} 實現(xiàn)三
線程安全的單例 -- (餓漢模式--簡潔、高效、不用加鎖、但是在某些場景下會有缺陷)
方法1
// 方式一
class Singleton
{
public:
// 獲取唯一對象實例的接口函數(shù)
static Singleton* GetInstance()
{
static Singleton sInstance;
return &sInstance;
}
void Print()
{
cout<<_data<Print();
} 方法2
// 方式二
class Singleton
{
public:
// 獲取唯一對象實例的接口函數(shù)
static Singleton* GetInstance()
{
assert(_sInstance);
return _sInstance;
}
// 刪除實例對象
static void DelInstance()
{
if (_sInstance)
{
delete _sInstance;
_sInstance = NULL;
}
}
void Print()
{
cout << _data << endl;
}
private:
// 構(gòu)造函數(shù)定義為私有,限制只能在類內(nèi)創(chuàng)建對象
Singleton()
:_data(0)
{}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
// 指向?qū)嵗闹羔樁x為靜態(tài)私有,這樣定義靜態(tài)成員函數(shù)獲取對象實例
static Singleton* _sInstance;
// 單例類里面的數(shù)據(jù)
int _data;
};
Singleton* Singleton::_sInstance = new Singleton;
void TestSingleton()
{
Singleton::GetInstance()->Print();
Singleton::DelInstance();
}帶RAII GC 自動回收實例對象的方式
class Singleton
{
public:
// 獲取唯一對象實例的接口函數(shù)
static Singleton* GetInstance()
{
assert(_sInstance);
return _sInstance;
}
// 刪除實例對象
static void DelInstance()
{
if (_sInstance)
{
delete _sInstance;
_sInstance = NULL;
}
}
void Print()
{
cout << _data << endl;
}
class GC
{
public:
~GC()
{
cout << "DelInstance()"<Print();
} 由于程序在結(jié)束的時候,系統(tǒng)會自動析構(gòu)所有的全局變量,實際上,系統(tǒng)也會析構(gòu)所有類的靜態(tài)成員變量,就像這些靜態(tài)變量是全局變量一樣。我們知道,靜態(tài)變量和全局變量在內(nèi)存中,都是存儲在靜態(tài)存儲區(qū)的,所以在析構(gòu)時,是同等對待的。

























另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。