C++ 单例模式
来源:互联网 发布:广州淘宝运营沛旭班 编辑:程序博客网 时间:2024/06/18 15:23
flyfish 2014-12-16
单例模式是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。
编写一个Singleton类
class Singleton(){}; 该类是空类经过编译器处理后,C++编译器默认编写的函数代码如下
class Singleton() {public:Singleton() {} // 1 构造函数Singleton(const Singleton& s{} // 2 拷贝构造函数~Singleton(){} // 3 析构函数Singleton& operator=(const Singleton& s{} // 4 拷贝赋值函数 copy assignment};
1 禁止用户建立对象
将Singleton类的构造函数,拷贝构造函数,拷贝赋值函数 全部声明为private,这样用户都没有权力建立对象
private:
Singleton() {};
Singleton(Singleton const&);
void operator=(Singleton const&);
2 用户可以调用
利用public成员函数来进行访问
3 线程安全,保证对象的唯一
静态成员对象(static member objects )是不是对象的一部分
从C++11 开始 如果静态成员声明为thread_local,每一个线程都有这样的一个object,否则在整个程序中的静态成员对象只有一个实例,也就是在C++11 如果声明了静态成员,那么这个静态成员是线程安全的。
对于什么时候创建对象有两种方案供选择
Eager evaluation和Lazy Evaluation
Eager evaluation是在程序启动时就要创建对象
最快的计算就是不计算,需要的话就是开始创建对象,不需要就不创建对象这就是惰性求值(Lazy Evaluation)
Lazy Evaluation的目的就是要最小化计算机要做的工作。
C++ 11 + Lazy Evaluation
实现如下
class Singleton{public:static Singleton& getInstance(){static Singleton instance;return instance;}private:Singleton() {};Singleton(Singleton const&);void operator=(Singleton const&);};编译器不支持C++11,可以参考Boost的实现
代码在boost文件夹的位置
boost\core\noncopyable.hpp
boost\serialization\singleton.hpp
简化可以看的清楚些
class noncopyable{protected:noncopyable() {}~noncopyable() {}private:noncopyable( const noncopyable& );noncopyable& operator=( const noncopyable& );};//noncopyable允许继承,//利用private声明禁止子类调用赋值和拷贝构造函数//利用protected声明禁止除了子类以外的其他类调用构造函数和析构函数//只能由子类调用构造和析构函数构造函数和析构函数
typedef noncopyable_::noncopyable noncopyable;class singleton_module : public boost::noncopyable{private:static bool & get_lock(){static bool lock = false;return lock;}public:static void lock(){get_lock() = true;}static void unlock(){get_lock() = false;}static bool is_locked() {return get_lock();}};template<class T>class singleton_wrapper : public T{public:static bool m_is_destroyed;~singleton_wrapper(){m_is_destroyed = true;}};template<class T>bool detail::singleton_wrapper< T >::m_is_destroyed = false;template <class T>class singleton : public singleton_module{private:static T & instance;static void use(T const &) {}static T & get_instance() {static detail::singleton_wrapper< T > t;BOOST_ASSERT(! detail::singleton_wrapper< T >::m_is_destroyed);use(instance);return static_cast<T &>(t);}public:static T & get_mutable_instance(){BOOST_ASSERT(! is_locked());return get_instance();}static const T & get_const_instance(){return get_instance();}static bool is_destroyed(){return detail::singleton_wrapper< T >::m_is_destroyed;}};template<class T>T & singleton< T >::instance = singleton< T >::get_instance();
C++11 利用 可变参数模板 实现的单例模式
#pragma oncetemplate <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){throw std::logic_error("Please initialize the instance first");}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&);private:static T* m_pInstance;};template < class T> T* Singleton<T>::m_pInstance = nullptr;//使用方式class A{public:void test(){};};//使用方式Singleton<A>::Instance();Singleton<A>::GetInstance()->test();Singleton<A>::DestroyInstance();
0 0
- C#单例模式
- C#-单例模式
- 单例模式(C++)
- 【C++】单例模式
- [C++]单例模式
- 单例模式(C++)
- C ++的单例模式
- 【C++】Chapter17:单例模式
- Object-c单例模式
- Objectove-c单例模式
- Objective C--单例模式
- Objective C--单例模式
- Objective C 单例模式
- Objective C--单例模式
- 单例模式(C#)
- 单例模式(C#)
- object-c 单例模式
- c++-单例模式Singleton
- 定期更新(推荐机器学习博客)
- 我是如何通过编程技术转换还金钱?
- OpenSSl 个人学习
- Java中thread的run()/start()方法以及Runnable接口
- 中文版Geneve02
- C++ 单例模式
- cellcli ?
- 智能推荐系统开发中的十个关键注意点
- Qt开发:Qt中打开网址
- Unity3D之迭代器
- getResponseCode()状态码
- linux命令选项 英文
- Linux常用C函数---字符测试篇
- How to Use beEF (Browser Exploitation Framework)