单例模式,是一种常用的软件设计模式。它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例。即一个类只有一个对象实例。
因此,实现单例模式需符合以下条件:
1.某个类只能有一个实例;
2.它必须自行创建这个实例;
3.它必须自行向整个系统提供这个实例。
下面基于Qt,实现6种方式的单例
以下方法概括了懒汉、饿汉、智能指针、线程安全、宏等实现,具体方法说明会持续更新。
方法一:为饿汉模式,只要类被加载,就会立刻进行实例化,创建时机比较早,“以空间换时间”,此方法是线程安全的
#ifndef SINGLETONONE
#define SINGLETONONE
class Singleton
{
public:
static Singleton* getInstance()
{
return instance;
}
public:
~Singleton(){}
private:
Singleton() = default;
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete;
Singleton(Singleton const&&) = delete;
Singleton& operator=(Singleton const&&) = delete;
private:
static Singleton *instance;
};
Singleton* Singleton::instance = new Singleton;
#endif // SINGLETONONE
方法二:当进行调用时,才创建其示例
#ifndef SINGLETONTWO
#define SINGLETONTWO
class Singleton
{
public:
static Singleton* getInstance()
{
static Singleton instance;
return &instance;
}
public:
~Singleton(){}
private:
Singleton() = default;
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete;
Singleton(Singleton const&&) = delete;
Singleton& operator=(Singleton const&&) = delete;
};
#endif // SINGLETONTWO
方法三:为懒汉模式,类被加载的时候,没有立刻被实例化,第一次调用getInstance的时候,才真正的实例化。
如果要是代码一整场都没有调用getInstance,此时实例化的过程也就被省略掉了,又称“延时加载”
一般认为“懒汉模式” 比 “饿汉模式”效率更高。
懒汉模式有很大的可能是“实例用不到”,此时就节省了实例化的开销。
#ifndef SINGLETONTHREE
#define SINGLETONTHREE
class Singleton
{
public:
static Singleton* getInstance()
{
if(!instance)
{
instance = new Singleton();
}
return instance;
}
public:
~Singleton(){}
private:
Singleton() = default;
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete;
Singleton(Singleton const&&) = delete;
Singleton& operator=(Singleton const&&) = delete;
private:
static Singleton *instance;
};
Singleton* Singleton::instance = nullptr;
#endif // SINGLETONTHREE
方法四:尤其懒汉模式是调用时进行实例化,所以当用于多线程时,是线程不安全的,因此可以进行双加锁。
#ifndef SINGLETONFOUR
#define SINGLETONFOUR
#include <QMutex>
#include <QMutexLocker>
#include <QScopedPointer>
class Singleton
{
public:
static Singleton* getInstance()
{
if(instance.isNull())
{
QMutexLocker locker(&mutex);
if(instance.isNull())
{
instance.reset(new Singleton);
}
}
return instance.data();
}
public:
~Singleton(){}
private:
Singleton() = default;
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete;
Singleton(Singleton const&&) = delete;
Singleton& operator=(Singleton const&&) = delete;
private:
static QScopedPointer<Singleton> instance;
static QMutex mutex;
};
QScopedPointer<Singleton> Singleton::instance;
QMutex Singleton::mutex;
#endif // SINGLETONFOUR
方法五:由于项目中用到单例的地方很多,所以可以将其抽象出来,实现成模板。
#ifndef SINGLETONFIVE
#define SINGLETONFIVE
#include <QMutex>
#include <QMutexLocker>
#include <QScopedPointer>
template <class T>
class Singleton
{
public:
static T* getInstance()
{
if(instance.isNull())
{
QMutexLocker locker(&mutex);
if(instance.isNull())
{
instance.reset(new T);
}
}
return instance.data();
}
public:
~Singleton(){}
protected:
Singleton() = default;
private:
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete;
Singleton(Singleton const&&) = delete;
Singleton& operator=(Singleton const&&) = delete;
private:
static QScopedPointer<T> instance;
static QMutex mutex;
};
template <class T>
QScopedPointer<T> Singleton<T>::instance;
template <class T>
QMutex Singleton<T>::mutex;
class C_xxx : public Singleton<C_xxx>
{
friend class Singleton<C_xxx>;
private:
C_xxx(){}
};
#endif // SINGLETONFIVE
方法六:在模板的基础上,进行宏定义,在需要调用的地方,调用DECLARE_SINGLETON(class)即可,方便实用。
#ifndef SINGLETONSIX
#define SINGLETONSIX
#include <QMutex>
#include <QMutexLocker>
#include <QScopedPointer>
template <class T>
class Singleton
{
public:
static T* getInstance()
{
if(instance.isNull())
{
QMutexLocker locker(&mutex);
if(instance.isNull())
{
instance.reset(new T);
}
}
return instance.data();
}
public:
~Singleton(){}
protected:
Singleton() = default;
private:
static QScopedPointer<T> instance;
static QMutex mutex;
};
template <class T>
QScopedPointer<T> Singleton<T>::instance;
template <class T>
QMutex Singleton<T>::mutex;
#define DECLARE_SINGLETON(Class) \
friend class Singleton<Class>; \
friend struct QScopedPointerDeleter<Class>; \
Q_DISABLE_COPY(Class) \
private: \
Class(Class const&&) = delete; \
Class& operator=(Class const&&) = delete; \
public: \
static Class* Instance() \
{ \
return Singleton<Class>::getInstance(); \
}
class C
{
DECLARE_SINGLETON(C)
private:
C(){}
};
#endif // SINGLETONSIX