单例模式——饿汉式和懒汉式
单例模式:一个类只允许存在唯一的对象,并提供它的访问方式
创建思路:
1.禁止在类的外部创建对象:私有化构造函数
2.在类的内部提供唯一的对象:静态成员变量
3.提供访问唯一对象的方法:静态成员函数
创建方式:
1.饿汉式:无论用或不用,陈晓旭启动即创建
2.懒汉式:用的时候创建,不用的时候销毁
1 //单例模式 饿汉式 2 //无论用不用,程序启动即创建 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Singleton{ 8 public: 9 static Singleton& getInstance(){ 10 return s_instance; 11 } 12 int getData(){ 13 return m_data; 14 } 15 16 private: 17 Singleton(int data=0):m_data(data){} 18 Singleton(const Singleton& that); 19 20 static Singleton s_instance; 21 int m_data; 22 }; 23 Singleton Singleton::s_instance(666); 24 25 int main(){ 26 Singleton& s = Singleton::getInstance(); 27 cout << s.getData()<< endl; 28 29 return 0; 30 }
1 //单例对象:懒汉式 2 //用时创建,不用就销毁,需要手动调用销毁 3 //如果是多个使用者,单例对象的析构函数应该是最后一个使用者调用,故加入计数 4 #include <iostream> 5 using namespace std; 6 7 class Singleton{ 8 public: 9 static Singleton& getInstance(){ 10 if(p_instance == NULL){ 11 p_instance = new Singleton(12345); 12 } 13 s_count++; 14 return *p_instance; 15 } 16 //单例对象不用即销毁 17 //单例对象可能有多个使用者,应该时最后一个使用者调用release才去delete 18 void release(){ 19 if(--s_count == 0 && p_instance != NULL){ 20 delete p_instance; 21 p_instance = NULL; 22 } 23 } 24 void print(){ 25 cout << m_data << endl; 26 } 27 private: 28 Singleton(int data=0):m_data(data){cout << "单例对象被创建了" << endl;} 29 Singleton(const Singleton& that); 30 ~Singleton(){cout << "单例对象被销毁了" << endl;} 31 32 int m_data; 33 static Singleton* p_instance; 34 static int s_count;//计数:记录单例使用者的个数 35 }; 36 Singleton* Singleton::p_instance = NULL; 37 int Singleton::s_count = 0; 38 39 int main(){ 40 cout << "main" << endl; 41 Singleton& s1 = Singleton::getInstance(); 42 cout << &s1 << endl; 43 s1.print(); 44 Singleton& s2 = Singleton::getInstance(); 45 cout << &s2 << endl; 46 s2.print(); 47 Singleton& s3 = Singleton::getInstance(); 48 cout << &s3 << endl; 49 s3.print(); 50 s3.release(); 51 s2.release(); 52 s1.release(); 53 54 return 0; 55 }
1 //单例模式:懒汉式 2 //将上面的代码用classA将其封装 3 //利用A的构造函数和析构函数,自动创建和释放 4 //完成了多线程的安全单例模式 5 #include <iostream> 6 #include <pthread.h> 7 #include <cstdio> 8 using std::cout; 9 using std::endl; 10 11 //静态初始化锁 12 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 13 14 class Singleton{ 15 public: 16 static Singleton& getInstance(){ 17 pthread_mutex_lock(&mutex); //加锁 18 if(p_instance == NULL){ 19 p_instance = new Singleton(666); 20 } 21 m_count++; 22 pthread_mutex_unlock(&mutex); //解锁 23 return *p_instance; 24 } 25 26 void destroy(){ 27 cout << "destroy" << endl; 28 pthread_mutex_lock(&mutex); //加锁 29 if(--m_count == 0 && p_instance != NULL){ 30 delete p_instance; 31 p_instance = NULL; 32 } 33 pthread_mutex_unlock(&mutex); //解锁 34 } 35 int getData(){ 36 return m_data; 37 } 38 private: 39 Singleton(int data=0):m_data(data){cout << "Singleton" << endl;} 40 Singleton(const Singleton& that); 41 ~Singleton(){cout << "~Singleton" << endl;} 42 43 static Singleton* p_instance; 44 int m_data; 45 static int m_count; 46 }; 47 48 Singleton* Singleton::p_instance = NULL; 49 int Singleton::m_count = 0; 50 51 class A{ 52 public: 53 A():s(Singleton::getInstance()){} 54 ~A(){s.destroy();} 55 int get(){ 56 return s.getData(); 57 } 58 private: 59 Singleton& s; 60 }; 61 62 void* funa(void* arg){ 63 A a; 64 cout << "funa" << endl; 65 } 66 67 void* funb(void* arg){ 68 A a; 69 cout << "funb" << endl; 70 } 71 72 int main(){ 73 pthread_t pa, pb;//创建2个线程 74 pthread_create(&pa,NULL,funa,NULL); 75 pthread_create(&pb,NULL,funb,NULL); 76 77 pthread_detach(pa); 78 pthread_detach(pb); 79 80 getchar(); 81 //等待,销毁线程 82 //pthread_join(pa,NULL); 83 //pthread_join(pb,NULL); 84 85 //销毁mutex锁 86 pthread_mutex_destroy(&mutex); 87 return 0; 88 }
转载于:https://www.cnblogs.com/qiuyuwutong/p/9417675.html
单例模式——饿汉式和懒汉式相关推荐
- 单例模式饿汉式与懒汉式详解
单例模式饿汉式与懒汉式详解 步骤: 私有构造方法,使得在类的外部不能调用此方法,限制产生多个对象 类初始化时,区分饿汉式与懒汉式的区别 对外部提供调用方法,将创建的对象返回,只能通过类来调用 饿汉式: ...
- Singleton模式(单例模式) 饿汉式和懒汉式
目的:整个应用中有且只有一个实例,所有指向该类型实例的引用都指向这个实例. 好比一个国家就只有一个皇帝(XXX),此时每个人叫的"皇帝"都是指叫的XXX本人; 常见单例模式类型: ...
- Java-Day12 面向对象的三大特征之封装、继承,单例模式(饿汉式、懒汉式)、方法重写 (覆盖)、注解 (annotation)、super关键字、对象的创建流程超详细
目录 1. 面向对象的三大特征之封装 1.1 封装的含义 1.2 封装的目的 1.3 封装的优点 1.4 封装的实现过程 1.5 拓展知识:JavaBean 2. 面向对象的三大特征之继承 2.1 继 ...
- Java中单例模式—饿汉式和懒汉式
所谓单例模式就是为了确保类在内存中只有一个对象,之所以会出现单例模式是因为,单例模式在系统内存中只存在一个对象,可以节约资源 目录 一.饿汉式 二.懒汉式 一.饿汉式 该方式在开发中是经常用的,因为这 ...
- 【学习笔记】【C++整理A】【总】单例模式-饿汉式、懒汉式;
文章目录 一.语言分类: 三.基础A:- 函数重载.默认参数.extern "C".重复包含 1.函数重载 2.默认参数 3.extern "C" 4.防止重复 ...
- 单例模式(饿汉式和懒汉式)
以前学习单例的时候,只理解了简单部分.这次看DRP,对单例的饿汉式和懒汉式有了一些认识和对比. 在实际的开发中,有些地方需要一个类只有一个实例.比如:网站在线人数的计数器,再比如IDE中的工具箱之类的 ...
- Java实现单例模式之饿汉式、懒汉式、枚举式,带测试。
Java实现单例的3种普遍的模式,饿汉式.懒汉式.枚举式. 具体代码如下: package com.lcx.mode;/*** * 饿汉式单例,不管以后用不用这个对象,我们一开始就创建这个对象的实例, ...
- 单例模式饿汉式/懒汉式的区别
単例设计模式(Singleton Pattern): 所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法. 如果我们要 ...
- 设计模式—单例模式(饿汉式、懒汉式)
目录 一.什么是单例模式? 二.单例模式的类型 三.单例模式的公共特征 四.单例模式-饿汉式 五.单例模式-懒汉式 5.1 懒汉式实现方式一(有问题不提倡使用) 5.2 懒汉式实现方式二(提倡使用) ...
最新文章
- 库克说AI也能创造很多就业岗位,苹果加码教育布局
- at命令不生效 linux_帮你精通Linux:简约却不简单的ls命令
- 有点长的 Java API 设计清单
- 微信公众平台开发(55)刮刮乐
- SpringBoot学习笔记(16):单元测试
- html代码 打开本地文件,打开本地HTML文件
- Qt官方文档阅读笔记-QStyledItemDelegate Class描述
- (王道408考研操作系统)第四章文件管理-第一节9:文件系统的层次结构
- pandas之在Excel上绘制柱形图
- ceph 存储 对比_对象存储cleversafe对比ceph有哪些优势?
- 西门子PLC S7-300紧凑型简介
- 错误报告函数:strerror和perror
- 橘子学设计模式之原型模式
- 为什么安装MathType无法复制粘贴
- oracle删除双引号,oracle 表名 双引号 删除
- 实用干货 !这才是职场最全实用建议
- spring4开发SpringBatch 样例 -配置文件版
- 宏转录组测序数据菌株层面的分析软件总结
- 【pandas】统计指标
- 京东CPS商品推广接入流程