//GOF
// Header file Singleton.h
class Singleton {
public:
static Singleton *Instance()
{
if( !m_pInstatnce)
m_pInstance = new Singleton;
return m_pInstance;
}
void DoSomething();
private:
static Singleton *m_pInstatnce=NULL;
private:
Singleton();
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
~Singleton();
}
//Meyers Singleton
// Header file Singleton.h
class Singleton
{
public:
static Singleton *Instance()
{
static Singleton sInstance;
return &sInstance;
}
private:
Singleton();
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
~Singleton();
}
// Implementation file Singleton.cpp
Singleton* Singleton::m_pInstance = 0;
//Doug Schmidt & Tim Harrison
//可多线程
// Header file Singleton.h
class Singleton {
public:
static Singleton &Instance()
{
if( !m_pInstatnce)
{
lock(m_mutex);
if( !m_pInstance )
m_pInstance = new Singleton;
UnLock(m_mutex);
}
return *m_pInstance;
}
private:
static volatitle Singleton *m_pInstatnce;
static Mutex m_mutex;private: Singleton();
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
~Singleton();
}
// Implementation file Singleton.cpp
Mutex Singleton::_mutex;Singleton *Singleton:m_pInstatnce = NULL;