单例模式之C++(Qt、六种实现方式)实现

单例模式,是一种常用的软件设计模式。它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例。即一个类只有一个对象实例。
因此,实现单例模式需符合以下条件:
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
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FreeLikeTheWind.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值