zoukankan      html  css  js  c++  java
  • Qt中单例模式的实现(4种方法)

    最简单的写法:

    1
    2
    3
    4
    5
    static MyClass* MyClass::Instance()
    {
    static MyClass inst;
    return &inst;
    }

    过去很长一段时间一直都这么写,简单粗暴有效。但是直接声明静态对象会使编译出的可执行文件增大,也有可能出现其他的一些问题,所以利用了Qt自带的智能指针QScopedPointer和线程锁QMutex,改成了需要时才动态初始化的模式:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    static MyClass* MyClass::Instance()
    {
    static QMutex mutex;
    static QScopedPointer<MyClass> inst;
    if (Q_UNLIKELY(!inst)) {
    mutex.lock();
    if (!inst) {
    inst.reset(new MyClass);
    }
    mutex.unlock();
    }
    return inst.data();
    }

    既保证了线程安全又防止了内存泄漏,效率也没降低太多,简直完美。

    可惜每次都要重复这么几行实在麻烦,于是写了一个模板类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    template <class T>
    class Singleton
    {
    public:
    static T* Instance()
    {
    static QMutex mutex;
    static QScopedPointer<T> inst;
    if (Q_UNLIKELY(!inst)) {
    mutex.lock();
    if (!inst) {
    inst.reset(new T);
    }
    mutex.unlock();
    }
    return inst.data();
    }
    };

    使用的时候直接这样——

    1
    MyClass* inst = Singleton<MyClass>::Instance();

    除了用模板类,还可以利用c++中强大的宏:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #define DECLARE_SINGLETON(Class) 
    Q_DISABLE_COPY(Class)
    public:
    static Class* Instance()
    {
    static QMutex mutex;
    static QScopedPointer<Class> inst;
    if (Q_UNLIKELY(!inst)) {
    mutex.lock();
    if (!inst) inst.reset(new Class);
    mutex.unlock();
    }
    return inst.data();
    }

    然后声明的时候,填加一行这个宏:

    1
    2
    3
    4
    5
    class MyClass
    {
    DECLARE_SINGLETON(MyClass); // 声明单例模式
    //...
    }

    好评好评。

    当然,为了要保证真的是单例模式,还要把构造函数限制为private,不然以后什么时候忘记了这码事,在外面又new了一下就不好了。

    另外Qt本身自带了一个宏Q_GLOBAL_STATIC,也有类似单例模式的效果,QThreadPool::globalInstance()函数的实现就是利用了这个宏。不过它的主要用处是声明全局变量,和Singleton还是有差别的。

    https://blog.yeatse.com/2015/03/03/qt-singleton-implementation/

  • 相关阅读:
    自动发送邮件功能
    工作中常用的Android系统ADB命令收集
    商城系统必须知道的【订单、优惠金额、退货、实际营收】解释
    商城系统项目必须知道的专业数据指标
    接口加密思路
    Selenium使用Chrome模拟手机浏览器方法解析
    PHP上传图片基本代码示例
    iframe子页面获取父页面的点击事件
    javascript实现网页倒计时效果
    Crontab常用命令总结
  • 原文地址:https://www.cnblogs.com/findumars/p/6204528.html
Copyright © 2011-2022 走看看