定义:确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例
特点:只有一个实例;自我实例化;提供全局访问点
优点:节约系统资源、提高系统效率,同时也能够严格控制客户对它的访问
缺点:违背了“单一职责原则”,同时也没有抽象类,所以扩展起来有一定的困难;在并发测试中,不利于代码调试
使用场合
- 当在系统中某个特定的类对象实例只需要有一个的时候,可以使用单例设计模式。需要注意的是,只有真正有“单一实例”的需求时才可使用
- Windows 的回收站、操作系统中的文件系统、多线程中的线程池、显卡的驱动程序对象、打印机的后台处理服务、应用程序的日志对象、数据库的连接池、网站的计数器、Web 应用的配置对象、应用程序中的对话框、系统中的缓存等常常被设计成单例
多例模式的实现原理
首先,在类中设置一个存储多个实例的容器,如ArrayList或HashMap;然后,静态初始化实例对象并添加到容器中;最后,根据条件返回容器中的实例对象
用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象
1. 饿汉式—-转自阿翔,用于自学
#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;// 单例模式:只允许实例化一个类// 饿汉式:main运行前就已经完成实例化class earth {int data;// 自己类型的一个静态成员static earth instance;// 构造函数为私有函数,外界不可调用earth(int val) : data(val) { printf("We create an earth\n"); }// 禁止拷贝构造earth(earth&) = delete;// 禁止拷贝earth& operator = (const earth&) = delete;void broadcast() {printf("We broadcast an message: Here is earth, my coordinates is solar system\n");}public:// 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)~earth() { printf("We destroy an earth\n"); }// 获取实例,所有线程获取的都是同一个实例static earth& getInstance() {return instance;}void hello() {printf("This is earth say hello\n");}int read_data() {return data;}void reset(int val) {data = val;}void try_to_broadcast(int threashold) {if (data == threashold) {broadcast();} else {printf("You don't have any authority to activate broadcasting to universe\n");}}};// 静态变量的初始化,这个初始化早于mainearth earth::instance(5);int main(){printf("We create an universe\n");earth &p = earth::getInstance();earth &q = earth::getInstance();p.hello();q.try_to_broadcast(7);q.reset(7);p.try_to_broadcast(7);printf("done\n");return 0;}
喊手撕的话可以写这个
#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;class earth {private:// 自己类型的一个静态成员static earth instance;// 构造函数为私有函数,外界不可调用earth() { printf("We create an earth\n"); }// 禁止拷贝构造earth(earth&) = delete;// 禁止拷贝earth& operator = (const earth&) = delete;public:// 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)~earth() {}// 获取实例,所有线程获取的都是同一个实例static earth& getInstance() {return instance;}};// 静态变量的初始化,这个初始化早于mainearth earth::instance;/******************************************************************************//******************************************************************************/# include<iostream># include<cstdlib>using namespace std;class A {private:static A one;A() {}A(A&) = delete;A& operator = (const A&) = delete;public:~A() {}static A& get() {return one;}};A A::one;
2. 懒汉式
版本1
#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;// 单例模式:只允许实例化一个类// 懒汉式:只有在调用时刻才会实例化,没有调用就不会实例化// 解决了线程安全的问题,多线程同时进行首次调用时也保证只会实例化一次class earth {int data;// 指向自己实例的一个指针,初始化NULLstatic earth *instance;// 构造函数为私有函数,外界不可调用earth(int val) : data(val) { printf("We create an earth\n"); }// 禁止拷贝构造earth(earth&) = delete;// 禁止拷贝earth& operator = (const earth&) = delete;void broadcast() {printf("We broadcast an message: Here is earth, my coordinates is solar system\n");}public:// 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)// 此处不会掉起析构函数,原因未知。~earth() { printf("We destroy an earth\n"); }// 获取实例,所有线程获取的都是同一个实例,并且只有在调用时才进行初始化// 多线程调用时,可能因为线程不同步的原因导致其被多次实例化的线程安全问题(这里不模拟这种情况static earth& getInstance() {if (instance == NULL) {instance = new earth(5);}return *instance;}void hello() {printf("This is earth say hello\n");}int read_data() {return data;}void reset(int val) {data = val;}void try_to_broadcast(int threashold) {if (data == threashold) {broadcast();} else {printf("You don't have any authority to activate broadcasting to universe\n");}}};// 将静态指针初始化为空earth* earth::instance = NULL;int main(){printf("We create an universe\n");earth& p = earth::getInstance();earth& q = earth::getInstance();p.hello();q.reset(7);p.try_to_broadcast(9);return 0;}
运行结果
We create an universeWe create an earthThis is earth say helloYou don't have any authority to activate broadcasting to universe
版本2
#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;// 单例模式:只允许实例化一个类// 懒汉式:只有在调用时刻才会实例化,没有调用就不会实例化// 解决了线程安全的问题,多线程同时进行首次调用时也保证只会实例化一次class earth {int data;// 构造函数为私有函数,外界不可调用earth(int val) : data(val) { printf("We create an earth\n"); }// 禁止拷贝构造earth(earth&) = delete;// 禁止拷贝earth& operator = (const earth&) = delete;void broadcast() {printf("We broadcast an message: Here is earth, my coordinates is solar system\n");}public:// 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)~earth() { printf("We destroy an earth\n"); }// 获取实例,所有线程获取的都是同一个实例,并且只有在调用时才进行初始化// 多线程调用时,编译器保证了函数进入后,初始化已经完成,不会进行多次实例化static earth& getInstance() {static earth instance(5);return instance;}void hello() {printf("This is earth say hello\n");}int read_data() {return data;}void reset(int val) {data = val;}void try_to_broadcast(int threashold) {if (data == threashold) {broadcast();} else {printf("You don't have any authority to activate broadcasting to universe\n");}}};int main(){printf("We create an universe\n");earth &p = earth::getInstance();earth &q = earth::getInstance();p.hello();q.reset(7);p.try_to_broadcast(9);return 0;}
运行结果
We create an universeWe create an earthThis is earth say helloYou don't have any authority to activate broadcasting to universeWe destroy an earth

