C++ 单例模式总结与剖析
本文转自:https://www.cnblogs.com/sunchaothu/p/10389842.htmlC++ 单例模式总结与剖析单例可能是最常用的简单的一种设计模式,实现方法多样,根据不同的需求有不同的写法; 同时单例也有其局限性,因此有很多人是反对使用单例的。本文对C++ 单例的常见写法进行了一个总结,包括懒汉式、线程安全、单例模板等; 按照从简单到复杂,最终回归简单的的方式循序渐进地介绍,并且对各种实现方法的局限进行了简单的阐述,大量用到了C++ 11的特性如智能指针,magic static,线程锁; 从头到尾理解下来,对于学习和巩固C++语言特性还是很有帮助的。本文的全部代码在 g++ 5.4.0 编译器下编译运行通过,可以在我的github 仓库中找到。 一、什么是单例单例 Singleton 是设计模式的一种,其特点是只提供唯一一个类的实例,具有全局变量的特点,在任何位置都可以通过接口获取到那个唯一实例;
二、C++单例的实现2.1 基础要点
2.2 C++ 实现单例的几种方式2.2.1 有缺陷的懒汉式懒汉式(Lazy-Initialization)的方法是直到使用时才实例化对象,也就说直到调用get_instance() 方法的时候才 new 一个单例的对象。好处是如果被调用就不会占用内存。 #include <iostream> // version1: // with problems below: // 1. thread is not safe // 2. memory leak class Singleton{ private: Singleton(){ std::cout<<"constructor called!"<<std::endl; } Singleton(Singleton&)=delete; Singleton& operator=(const Singleton&)=delete; static Singleton* m_instance_ptr; public: ~Singleton(){ std::cout<<"destructor called!"<<std::endl; } static Singleton* get_instance(){ if(m_instance_ptr==nullptr){ m_instance_ptr = new Singleton; } return m_instance_ptr; } void use() const { std::cout << "in use" << std::endl; } }; Singleton* Singleton::m_instance_ptr = nullptr; int main(){ Singleton* instance = Singleton::get_instance(); Singleton* instance_2 = Singleton::get_instance(); return 0; }
运行的结果是 constructor called!
可以看到,获取了两次类的实例,却只有一次类的构造函数被调用,表明只生成了唯一实例,这是个最基础版本的单例实现,他有哪些问题呢?
因此,这里提供一个改进的,线程安全的、使用智能指针的实现; 2.2.2 线程安全、内存安全的懒汉式单例 (智能指针,锁)#include <iostream> #include <memory> // shared_ptr #include <mutex> // mutex // version 2: // with problems below fixed: // 1. thread is safe now // 2. memory doesn‘t leak class Singleton{ public: typedef std::shared_ptr<Singleton> Ptr; ~Singleton(){ std::cout<<"destructor called!"<<std::endl; } Singleton(Singleton&)=delete; Singleton& operator=(const Singleton&)=delete; static Ptr get_instance(){ // "double checked lock" if(m_instance_ptr==nullptr){ std::lock_guard<std::mutex> lk(m_mutex); if(m_instance_ptr == nullptr){ m_instance_ptr = std::shared_ptr<Singleton>(new Singleton); } return m_instance_ptr; } } private: Singleton(){ std::cout<<"constructor called!"<<std::endl; } static Ptr m_instance_ptr; static std::mutex m_mutex; }; // initialization static variables out of class Singleton::Ptr Singleton::m_instance_ptr = nullptr; std::mutex Singleton::m_mutex; int main(){ Singleton::Ptr instance = Singleton::get_instance(); Singleton::Ptr instance2 = Singleton::get_instance(); return 0; }
运行结果如下,发现确实只构造了一次实例,并且发生了析构。 constructor called! destructor called!
shared_ptr和mutex都是C++11的标准,以上这种方法的优点是
不足之处在于: 使用智能指针会要求用户也得使用智能指针,非必要不应该提出这种约束; 使用锁也有开销; 同时代码量也增多了,实现上我们希望越简单越好。 还有更加严重的问题,在某些平台(与编译器和指令集架构有关),==双检锁会失效==!具体可以看这篇文章,解释了为什么会发生这样的事情。 因此这里还有第三种的基于 Magic Staic的方法达到线程安全 2.2.3 最推荐的懒汉式单例(magic static )——局部静态变量#include <iostream> class Singleton { public: ~Singleton(){ std::cout<<"destructor called!"<<std::endl; } Singleton(const Singleton&)=delete; Singleton& operator=(const Singleton&)=delete; static Singleton& get_instance(){ static Singleton instance; return instance; } private: Singleton(){ std::cout<<"constructor called!"<<std::endl; } }; int main(int argc,char *argv[]) { Singleton& instance_1 = Singleton::get_instance(); Singleton& instance_2 = Singleton::get_instance(); return 0; }
运行结果 constructor called! destructor called!
这种方法又叫做 Meyers‘ SingletonMeyer‘s的单例, 是著名的写出《Effective C++》系列书籍的作者 Meyers 提出的。所用到的特性是在C++11标准中的Magic Static特性:
这样保证了并发线程在获取静态局部变量的时候一定是初始化过的,所以具有线程安全性。 C++静态变量的生存期?是从声明到程序结束,这也是一种懒汉式。 这是最推荐的一种单例实现方式:
另外网上有人的实现返回指针而不是返回引用 static Singleton* get_instance(){ static Singleton instance; return &instance; }
这样做并不好,理由主要是无法避免用户使用 2.2.4 函数返回引用有人在网上提供了这样一种单例的实现方式; #include <iostream> class A { public: A() { std::cout<<"constructor" <<std::endl; } ~A(){ std::cout<<"destructor"<<std::endl; } }; A& ret_singleton(){ static A instance; return instance; } int main(int argc,char *argv[]) { A& instance_1 = ret_singleton(); A& instance_2 = ret_singleton(); return 0; }
严格来说,这不属于单例了,因为类A只是个寻常的类,可以被定义出多个实例,但是亮点在于提供了 以上是各种方法实现单例的代码和说明,解释了各种技术实现的初衷和原因。这里会比较推荐 C++11 标准下的 2.2.3 的方式,即使用static local的方法,简单的理由来说是因为其足够简单却满足所有需求和顾虑。 在某些情况下,我们系统中可能有多个单例,如果都按照这种方式的话,实际上是一种重复,有没有什么方法可以只实现一次单例而能够复用其代码从而实现多个单例呢? 很自然的我们会考虑使用模板技术或者继承的方法, 2.3 单例的模板2.3.1 CRTP 奇异递归模板模式实现代码示例如下: // brief: a singleton base class offering an easy way to create singleton #include <iostream> template<typename T> class Singleton{ public: static T& get_instance(){ static T instance; return instance; } virtual ~Singleton(){ std::cout<<"destructor called!"<<std::endl; } Singleton(const Singleton&)=delete; Singleton& operator =(const Singleton&)=delete; protected: Singleton(){ std::cout<<"constructor called!"<<std::endl; } }; /********************************************/ // Example: // 1.friend class declaration is requiered! // 2.constructor should be private class DerivedSingle:public Singleton<DerivedSingle>{ // !!!! attention!!! // needs to be friend in order to // access the private constructor/destructor friend class Singleton<DerivedSingle>; public: DerivedSingle(const DerivedSingle&)=delete; DerivedSingle& operator =(const DerivedSingle&)= delete; private: DerivedSingle()=default; }; int main(int argc,char* argv[]){ DerivedSingle& instance1 = DerivedSingle::get_instance(); DerivedSingle& instance2 = DerivedSingle::get_instance(); return 0; }
以上实现一个单例的模板基类,使用方法如例子所示意,子类需要将自己作为模板参数T?传递给? 基类模板的实现要点是:
2.3.2 不需要在子类声明友元的实现方法在?stackoverflow上, 有大神给出了不需要在子类中声明友元的方法,在这里一并放出;精髓在于使用一个代理类 token,子类构造函数需要传递token类才能构造,但是把 token保护其起来, 然后子类的构造函数就可以是公有的了,这个子类只有? // brief: a singleton base class offering an easy way to create singleton #include <iostream> template<typename T> class Singleton{ public: static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value){ static T instance{token()}; return instance; } virtual ~Singleton() =default; Singleton(const Singleton&)=delete; Singleton& operator =(const Singleton&)=delete; protected: struct token{}; // helper class Singleton() noexcept=default; }; /********************************************/ // Example: // constructor should be public because protected `token` control the access class DerivedSingle:public Singleton<DerivedSingle>{ public: DerivedSingle(token){ std::cout<<"destructor called!"<<std::endl; } ~DerivedSingle(){ std::cout<<"constructor called!"<<std::endl; } DerivedSingle(const DerivedSingle&)=delete; DerivedSingle& operator =(const DerivedSingle&)= delete; }; int main(int argc,char* argv[]){ DerivedSingle& instance1 = DerivedSingle::get_instance(); DerivedSingle& instance2 = DerivedSingle::get_instance(); return 0; }
2.3.3 函数模板返回引用在 2.2.4 中提供了一种类型的全局变量的方法,可以把一个一般的类,通过这种方式提供一个类似单例的 #include <iostream> class A { public: A() { std::cout<<"constructor" <<std::endl; } ~A(){ std::cout<<"destructor"<<std::endl; } }; template<typename T> T& get_global(){ static T instance; return instance; } int main(int argc,char *argv[]) { A& instance_1 = get_global<A>(); A& instance_2 = get_global<A>(); return 0; }
可以看到这种方式确实非常简洁,同时类仍然具有一般类的特点而不受限制,当然也因此失去了单例那么强的约束(禁止赋值、构造和拷贝构造)。 三、何时应该使用或者不使用单例根据stackoverflow上的一个高票答案?singleton-how-should-it-be-used:
反对单例的理由当然程序员是分流派的,有些是反对单例的,有些人是反对设计模式的,有些人甚至连面向对象都反对 :). 反对单例的理由有哪些: 参考文章在本文写作的过程中参考了一些博客和stackoverflow 的回答,以超链接的方式体现在文中。另外还有一些我觉得非常精彩的回答,放在下面供读者拓展阅读 推荐阅读:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |