变参模板改进单例模式
单例模式保证一个类仅有一个实例,并提供一个访问它的全局访问点
泛型单例模式需要变参构造函数,构造函数的参数个数需要支持变化
下面是不用变参模板,支持0~6个参数的单例模式实现
#include <iostream> // 泛型单例模式需要变参构造函数,构造函数的参数个数需要支持变化 // 支持0~6个参数的单例模式实现 template <typename T> class Singleton { public: // 支持0个参数的构造函数 static T *Instance() { if (m_pInstance == nullptr) m_pInstance = new T(); return m_pInstance; } // 支持1个参数的构造函数 template <typename T0> static T *Instance(T0 arg0) { if (m_pInstance == nullptr) m_pInstance = new T(arg0); return m_pInstance; } // 支持2个参数的构造函数 template <typename T0, typename T1> static T *Instance(T0 arg0, T1 arg1) { if (m_pInstance == nullptr) m_pInstance = new T(arg0, arg1); return m_pInstance; } // 支持3个参数的构造函数 template <typename T0, typename T1, typename T2> static T *Instance(T0 arg0, T1 arg1, T2 arg2) { if (m_pInstance == nullptr) m_pInstance = new T(arg0, arg1, arg2); return m_pInstance; } // 支持4个参数的构造函数 template <typename T0, typename T1, typename T2, typename T3> static T *Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3) { if (m_pInstance == nullptr) m_pInstance = new T(arg0, arg1, arg2, arg3); return m_pInstance; } // 支持5个参数的构造函数 template <typename T0, typename T1, typename T2, typename T3, typename T4> static T *Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { if (m_pInstance == nullptr) m_pInstance = new T(arg0, arg1, arg2, arg3, arg4); return m_pInstance; } // 支持6个参数的构造函数 template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5> static T *Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { if (m_pInstance == nullptr) m_pInstance = new T(arg0, arg1, arg2, arg3, arg4, arg5); return m_pInstance; } // 获取单例 static T GetInstance() { if (m_pInstance == nullptr) { std::cout << "the instance is not init, please initialize the instance first" << std::endl; return nullptr; } return m_pInstance; } // 释放单例 static void DestroyInstance() { delete m_pInstance; m_pInstance = nullptr; } private: // 不允许赋值和复制 Singleton(void); virtual ~Singleton(void); Singleton(const Singleton &); Singleton &operator=(const Singleton &); static T *m_pInstance; }; template <class T> T *Singleton<T>::m_pInstance = nullptr; // test data struct A { A() {} }; struct B { B(int x) {} }; struct C { C(int x, double y) {} }; int main(void) { Singleton<A>::Instance(); Singleton<B>::Instance(1); Singleton<C>::Instance(1, 3.14); Singleton<A>::DestroyInstance(); Singleton<B>::DestroyInstance(); Singleton<C>::DestroyInstance(); return 0; }
通过变参模板改进单例模式
#include <iostream> #include <string> template <typename T> class Singleton { public: template <typename... Args> static T *Instance(Args &&...args) { if (m_pInstance == nullptr) m_pInstance = new T(std::forward<Args>(args)...); return m_pInstance; } // 获取单例 static T *GetInstance() { if (m_pInstance == nullptr) { std::cout << "the instance is not init, please initialize the instance first" << std::endl; return nullptr; } return m_pInstance; } static void DestoryInstance() { delete m_pInstance; m_pInstance = nullptr; } private: Singleton(void); virtual ~Singleton(void); Singleton(const Singleton &); Singleton &operator=(const Singleton &); static T *m_pInstance; }; template <typename T> T *Singleton<T>::m_pInstance = nullptr; // test data struct A { A(const std::string &) { std::cout << "lvalue" << std::endl; } A(std::string &&x) { std::cout << "rvalue" << std::endl; } }; struct B { B(const std::string &) { std::cout << "lvalue" << std::endl; } B(std::string &&x) { std::cout << "rvalue" << std::endl; } }; struct C { C(int x, double y) {} void Fun() { std::cout << "test" << std::endl; } }; int main(void) { std::string str = "bb"; // 创建A类型的单例 Singleton<A>::Instance(str); // 创建B类型的单例 Singleton<B>::Instance(std::move(str)); // 创建C类型的单例 Singleton<C>::Instance(1, 3.14); // 获取单例并调用单例对象的方法 Singleton<C>::GetInstance()->Fun(); // 释放单例 Singleton<A>::DestoryInstance(); Singleton<B>::DestoryInstance(); Singleton<C>::DestoryInstance(); }
编译运行
无情的摸鱼机器