c++設計模式:單件模式(Singleton Pattern)

來源:互聯網
上載者:User

定義:單件模式確保一個類只有一個執行個體,並提供一個全域訪問點

實現一:

#include <iostream>
using namespace std;

class CSingleton
{
public:
static CSingleton* getInstance();
static void cleanInstance();
int getValue();
void setValue(int iValue);
private:
int m_iValue;
static CSingleton* m_pSingleton;
CSingleton();
~CSingleton();
};

CSingleton* CSingleton::m_pSingleton = NULL;

CSingleton::CSingleton()
{
cout << "Constructor" << endl;
}

CSingleton::~CSingleton()
{
cout << "Destructor" << endl;
}

CSingleton* CSingleton::getInstance()
{
if (NULL == m_pSingleton)
{
m_pSingleton = new CSingleton();
}
return m_pSingleton;
}

void CSingleton::cleanInstance()
{
delete m_pSingleton;
}

int CSingleton::getValue()
{
return m_iValue;
}

void CSingleton::setValue(int iValue)
{
m_iValue = iValue;
}

int main()
{
CSingleton* pSingleton1 = CSingleton::getInstance();
CSingleton* pSingleton2 = CSingleton::getInstance();
pSingleton1->setValue(123);
if (pSingleton1->getValue() == pSingleton2->getValue())
{
cout << "Two objects is the same instance" << endl;
}
else
{
cout << "Two objects isn't the same instance" << endl;
}

CSingleton::cleanInstance();
return 0;
}

相信大多數的同仁都喜歡使用上邊這種單件模式的實現方法,如果在單線程的情況下,是沒有問題的,但如果是多線程,那麼就極有可能會返回兩個不同的對象,在調用

CSingleton::getInstance的時候,兩個線程如果都同時運行完if判斷,而又還沒有調用到建構函式的話,想象下後果吧。那該怎麼辦呢?看下邊這個實現吧。
實現二:
#include <iostream>
using namespace std;

class CSingleton
{
public:
static CSingleton* getInstance();
static void cleanInstance();
int getValue();
void setValue(int iValue);
private:
int m_iValue;
static CSingleton* m_pSingleton;
CSingleton();
~CSingleton();
};

// 在進程運行開始就執行個體化該單件,又稱“急切”建立執行個體
CSingleton* CSingleton::m_pSingleton = new CSingleton();

CSingleton::CSingleton()
{
cout << "Constructor" << endl;
}

CSingleton::~CSingleton()
{
cout << "Destructor" << endl;
}

CSingleton* CSingleton::getInstance()
{
return m_pSingleton;
}

void CSingleton::cleanInstance()
{
delete m_pSingleton;
}

int CSingleton::getValue()
{
return m_iValue;
}

void CSingleton::setValue(int iValue)
{
m_iValue = iValue;
}

int main()
{
CSingleton* pSingleton1 = CSingleton::getInstance();
CSingleton* pSingleton2 = CSingleton::getInstance();
pSingleton1->setValue(123);
if (pSingleton1->getValue() == pSingleton2->getValue())
{
cout << "Two objects is the same instance" << endl;
}
else
{
cout << "Two objects isn't the same instance" << endl;
}

CSingleton::cleanInstance();
return 0;
}
哈哈,看清楚了嗎?就是在進程啟動並執行時候就對這個單件進行執行個體化,可是這樣似乎又不能達到延遲執行個體化的目的啦。如果我們的對象對資源佔用非常大,而我們的進行在整個過程中其實並沒有用到這個單件,那豈不是白白浪費資源了嘛。還有更好的辦法。
實現三:
#include <iostream>
using namespace std;

class CSingleton
{
public:
static CSingleton* getInstance();
int getValue();
void setValue(int iValue);
private:
int m_iValue;
CSingleton();
~CSingleton();
};

CSingleton::CSingleton()
{
cout << "Constructor" << endl;
}

CSingleton::~CSingleton()
{
cout << "Destructor" << endl;
}

CSingleton* CSingleton::getInstance()
{
static CSingleton single;
return &single;
}

int CSingleton::getValue()
{
return m_iValue;
}

void CSingleton::setValue(int iValue)
{
m_iValue = iValue;
}

int main()
{
cout << "Process begin" << endl;
CSingleton* pSingleton1 = CSingleton::getInstance();
CSingleton* pSingleton2 = CSingleton::getInstance();
pSingleton1->setValue(123);
if (pSingleton1->getValue() == pSingleton2->getValue())
{
cout << "Two objects is the same instance" << endl;
}
else
{
cout << "Two objects isn't the same instance" << endl;
}
return 0;
}
看下運行結果吧:

Process begin
Constructor
Two objects is the same instance
Destructor

是不是跟預想的一樣呢?把單件聲明為成員函數中的靜態成員,這樣既可以達到延遲執行個體化的目的,又能達到安全執行緒的目的,而且看結果的最後,是不是在程式退出的時候,還自動的調用了解構函式呢?這樣我們就可以把資源的釋放放到解構函式裡邊,等到程式退出的時候,進程會自動釋放這些靜態成員的。

 
參考圖書:《Head First 設計模式》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.