如何实现设计模式中的单例模式?

2026-05-05 17:131阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计440个文字,预计阅读时间需要2分钟。

如何实现设计模式中的单例模式?

说明:单例模式目的:为了保证一个类在程序中只有一个实例,并且能被全局访问。场景:全局线程池要点:全局性:使用static关键字全局性:使用static关键字多线程:使用mutex进行保护

说明

目的:为了保证一个类在程序中只有一个实例,并且能被全局访问

场景:全局线程池

要点:

  1. 全局性:使用static关键字,变量需要存在于静态存储区
  2. 多线程:使用mutex进行保护
  3. 唯一性:只生成一个实例
实现 单例类

通过 Test::Instance()获取类指针

class Test { public: static Test* Instance() { if (m_instance == nullptr) { std::lock_guard<std::mutex> lock(m_mutex); if (m_instance == nullptr) m_instance = new Test; } return m_instance; } static void DeleteInstance() { if (m_instance) { std::lock_guard<std::mutex> lock(m_mutex); if (m_instance) { delete m_instance; m_instance = nullptr; } } } private: Test() {} private: static Test *m_instance; static std::mutex m_mutex; }; Test* Test::m_instance = NULL; std::mutex Test::m_mutex; 单例宏

每个类想要实现单例模式,都要写一遍Instance的接口,有点麻烦,于是希望能用宏实现单例模式,最终使用效果为:

如何实现设计模式中的单例模式?

class Test { SINGELTON(Test) private: Test() {} };

可以用类模板的方式来进行:

template<class T> class Singleton { public: static T *Instance() { if (m_instance == nullptr) { std::lock_guard<std::mutex> lock(m_instanceMutex); if (m_instance == nullptr) m_instance = std::unique_ptr<T>(new T); } return m_instance.get(); } private: static std::unique_ptr<T> m_instance; static std::mutex m_instanceMutex; }; template<class T> std::mutex Singleton<T>::m_instanceMutex; template<class T> std::unique_ptr<T> Singleton<T>::m_instance(nullptr); //单例宏 #define SINGELTON(OBJ_CLASS) \ friend Singleton<OBJ_CLASS>; \ \ public: \ static OBJ_CLASS *Instance() \ { \ return Singleton<OBJ_CLASS>::Instance(); \ }

标签:为了保证

本文共计440个文字,预计阅读时间需要2分钟。

如何实现设计模式中的单例模式?

说明:单例模式目的:为了保证一个类在程序中只有一个实例,并且能被全局访问。场景:全局线程池要点:全局性:使用static关键字全局性:使用static关键字多线程:使用mutex进行保护

说明

目的:为了保证一个类在程序中只有一个实例,并且能被全局访问

场景:全局线程池

要点:

  1. 全局性:使用static关键字,变量需要存在于静态存储区
  2. 多线程:使用mutex进行保护
  3. 唯一性:只生成一个实例
实现 单例类

通过 Test::Instance()获取类指针

class Test { public: static Test* Instance() { if (m_instance == nullptr) { std::lock_guard<std::mutex> lock(m_mutex); if (m_instance == nullptr) m_instance = new Test; } return m_instance; } static void DeleteInstance() { if (m_instance) { std::lock_guard<std::mutex> lock(m_mutex); if (m_instance) { delete m_instance; m_instance = nullptr; } } } private: Test() {} private: static Test *m_instance; static std::mutex m_mutex; }; Test* Test::m_instance = NULL; std::mutex Test::m_mutex; 单例宏

每个类想要实现单例模式,都要写一遍Instance的接口,有点麻烦,于是希望能用宏实现单例模式,最终使用效果为:

如何实现设计模式中的单例模式?

class Test { SINGELTON(Test) private: Test() {} };

可以用类模板的方式来进行:

template<class T> class Singleton { public: static T *Instance() { if (m_instance == nullptr) { std::lock_guard<std::mutex> lock(m_instanceMutex); if (m_instance == nullptr) m_instance = std::unique_ptr<T>(new T); } return m_instance.get(); } private: static std::unique_ptr<T> m_instance; static std::mutex m_instanceMutex; }; template<class T> std::mutex Singleton<T>::m_instanceMutex; template<class T> std::unique_ptr<T> Singleton<T>::m_instance(nullptr); //单例宏 #define SINGELTON(OBJ_CLASS) \ friend Singleton<OBJ_CLASS>; \ \ public: \ static OBJ_CLASS *Instance() \ { \ return Singleton<OBJ_CLASS>::Instance(); \ }

标签:为了保证