C++常见设计模式——单例模式

mac2024-05-19  32

设计模式是对设计原则的具体化。用江湖话说就是武林秘籍,总结出来的一些固定套路,可以帮助有根基的程序员迅速打通任督二脉,从此做什么都特别快。

C++常见设计模式——单例模式

单例模式懒汉单例模式非线程安全的懒汉单例模式线程安全的懒汉单例模式返回一个reference指向local static对象 饿汉单例模式

单例模式

单例模式是一种常用的软件设计模式。

在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。

应用场景:如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

实现单例模式必须注意一下几点: ● 单例类只能由一个实例化对象。 ● 单例类必须自己提供一个实例化对象。 ● 单例类必须提供一个可以访问唯一实例化对象的接口。

单例模式分为懒汉和饿汉两种实现方式。

懒汉单例模式

懒汉:故名思义,不到万不得已就不会去实例化类,也就是说在第一次用到类实例的时候才会去实例化一个对象。在访问量较小,甚至可能不会去访问的情况下,采用懒汉实现,这是以时间换空间。

非线程安全的懒汉单例模式

/* * 关键代码:构造函数是私有的,不能通过赋值运算,拷贝构造等方式实例化对象。 *///懒汉式一般实现:非线程安全,getInstance返回的实例指针需要delete class Singleton { public: static Singleton* getInstance(); ~Singleton(){}private: Singleton(){} //构造函数私有 Singleton(const Singleton& obj) = delete; //明确拒绝 Singleton& operator=(const Singleton& obj) = delete; //明确拒绝 static Singleton* m_pSingleton; }; ​ Singleton* Singleton::m_pSingleton = NULL; ​ Singleton* Singleton::getInstance() { if(m_pSingleton == NULL) { m_pSingleton = new Singleton; } return m_pSingleton; }

线程安全的懒汉单例模式

std::mutex mt; class Singleton { public: static Singleton* getInstance(); private: Singleton(){} //构造函数私有 Singleton(const Singleton&) = delete; //明确拒绝 Singleton& operator=(const Singleton&) = delete; //明确拒绝 static Singleton* m_pSingleton; }; Singleton* Singleton::m_pSingleton = NULL; Singleton* Singleton::getInstance() { if(m_pSingleton == NULL) { mt.lock(); if(m_pSingleton == NULL) { m_pSingleton = new Singleton(); } mt.unlock(); } return m_pSingleton; }

返回一个reference指向local static对象

这种单例模式实现方式多线程可能存在不确定性:任何一种non-const static对象,不论它是local或non-local,在多线程环境下“等待某事发生”都会有麻烦。解决的方法:在程序的单线程启动阶段手工调用所有reference-returning函数。这种实现方式的好处是不需要去delete它。

class Singleton { public: static Singleton& getInstance(); private: Singleton(){} Singleton(const Singleton&) = delete; //明确拒绝 Singleton& operator=(const Singleton&) = delete; //明确拒绝 }; ​ ​ Singleton& Singleton::getInstance() { static Singleton singleton; return singleton; }

饿汉单例模式

饿汉:饿了肯定要饥不择食。所以在单例类定义的时候就进行实例化。在访问量比较大,或者可能访问的线程比较多时,采用饿汉实现,可以实现更好的性能。这是以空间换时间。

//饿汉式:线程安全,注意一定要在合适的地方去delete它 class Singleton { public: static Singleton* getInstance(); private: Singleton(){} //构造函数私有 Singleton(const Singleton&) = delete; //明确拒绝 Singleton& operator=(const Singleton&) = delete; //明确拒绝static Singleton* m_pSingleton; }; ​ Singleton* Singleton::m_pSingleton = new Singleton(); ​ Singleton* Singleton::getInstance() { return m_pSingleton; }
最新回复(0)