C++单例模式的实现及举例
单例模式的概念和用途:
在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便实例个数的控制并节约系统资源。
如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。(直白地说单例模式的写法也是种套路,见例子便知。)
举例:
1. 主席
目的:为了让类中只有一个实例,实例不需要自己释放
• 将 默认构造 和 拷贝构造 私有化
• 内部维护一个 对象指针
• 私有化唯一指针
• 对外提供getinstance方法来访问这个指针
• 保证类中只能实例化一个对象
- #include<iostream>
- using namespace std;
- //创建主席类
- //需求 单例模式 为了创建类中的对象,并且保证只有一个对象实例
- class ChairMan {
- private:
- ChairMan()
- {
- cout << "创建主席" << endl;
- }
- //拷贝构造 私有化
- ChairMan(const ChairMan&c) {}
- public:
- //提供 get方法 访问 主席
- static ChairMan* getInstance() {
- return singleMan;
- }
- private:
- static ChairMan * singleMan;
- };
- ChairMan * ChairMan::singleMan = new ChairMan;
- void test01() {
- ChairMan *cm1 = ChairMan::getInstance();
- ChairMan *cm2 = ChairMan::getInstance();
- if (cm1 == cm2) {
- cout << "cm1与cm2相同" << endl;
- }
- else cout << "cm1与cm2不同" << endl;
- /*ChairMan *cm3 = new ChairMan(*cm2);
- if (cm3 == cm2) {
- cout << "cm3与cm2相同" << endl;
- }
- else cout << "cm3与cm2不同" << endl;*/
- }
- int main() {
- cout << "main调用" << endl; //主席创建先于main调用
- test01();
- system("pause");
- return ;
- }
2. 打印机
用单例模式,模拟公司员工使用打印机场景,打印机可以打印员工要输出的内容,并且可以累积打印机使用次数。
- #include<iostream>
- #include<string>
- using namespace std;
- class Printer {
- private:
- Printer() { m_Count = ; };
- Printer(const Printer& p);
- public:
- static Printer* getInstance() {
- return singlePrinter;
- }
- void printText(string text) {
- cout << text << endl;
- m_Count++;
- cout << "打印机使用次数:" << m_Count << endl;
- }
- private:
- static Printer* singlePrinter;
- int m_Count;
- };
- Printer* Printer::singlePrinter = new Printer;
- void test1() {
- //拿到打印机
- Printer* printer = Printer::getInstance();
- printer->printText("离职报告");
- }
- int main() {
- test1();
- system("pause");
- return ;
- }
3. 模板
- #include <iostream>
- using namespace std;
- class Singleton
- {
- private:
- Singleton()
- {
- cout << "Singleton" << endl;
- }
- ~Singleton()
- {
- cout << "~Singleton" << endl;
- }
- private:
- static Singleton* _pInstance;
- public:
- static Singleton * getInstance()
- {
- if(nullptr == _pInstance)
- {
- _pInstance = new Singleton;
- }
- return _pInstance;
- }
- static void destroy()
- {
- if(_pInstance)
- {
- delete _pInstance;
- }
- }
- void print() const
- {
- cout << "Singleton::print()" << endl;
- }
- };
- Singleton* Singleton::_pInstance = nullptr;
- int main()
- {
- Singleton * p1 = Singleton::getInstance();
- Singleton * p2 = Singleton::getInstance();
- p1->print();
- p2->print();
- Singleton::getInstance()->print();
- Singleton::destroy();
- return ;
- }
C++单例模式的实现及举例的更多相关文章
- 如何防止JAVA反射对单例类的攻击?
在我的上篇随笔中,我们知道了创建单例类有以下几种方式: (1).饿汉式; (2).懒汉式(.加同步锁的懒汉式.加双重校验锁的懒汉式.防止指令重排优化的懒汉式); (3).登记式单例模式; (4).静态 ...
- 谈谈OKHttp的几道面试题
来吧,今天说说常用的网络框架OKHttp,也是现在Android所用的原生网络框架(Android 4.4开始,HttpURLConnection的底层实现被Google改成了OkHttp),GOGO ...
- Android面试题《思考与解答》11月刊
又来更新啦,Android面试题<思考与解答>11月刊奉上. 说说View/ViewGroup的绘制流程 View的绘制流程是从ViewRoot的performTraversals开始的, ...
- 真实的C++单例模式举例
把构造函数声明为protected的理由很简单,但把构造函数声明为private的原因却很少知道. 从语法上讲,任何函数如果被声明为private,这个函数就不能从外部调用,构造函数也是函数,相反 ...
- PHP 面向对象编程和设计模式 (3/5) - 单例模式和工厂模式
PHP高级程序设计 学习笔记 2014.06.11 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容 ...
- 设计模式之单例模式Singleton(三创建型)
1.什么事单例模式? 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建自己的唯一实例. 3.单例类必须 ...
- 设计模式(2)--单例模式(Singleton Pattern)
概述 一个类能返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个名称):当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的 ...
- C#设计模式-单例模式
单例模式三种写法: 第一种最简单,但没有考虑线程安全,在多线程时可能会出问题…… public class Singleton { private static Singleton _instance ...
- 【JAVA单例模式详解】
设计模式是一种思想,适合于任何一门面向对象的语言.共有23种设计模式. 单例设计模式所解决的问题就是:保证类的对象在内存中唯一. 举例: A.B类都想要操作配置文件信息Config.java,所以在方 ...
随机推荐
- hdu4280 Island Transport 最大流
In the vast waters far far away, there are many islands. People are living on the islands, and all t ...
- 线程---插队和礼让执行(join和yield)
插队: 礼让:
- 【java编程】Java魔法类:Unsafe应用解析
转载来源:https://tech.meituan.com/2019/02/14/talk-about-java-magic-class-unsafe.html 前言 Unsafe是位于sun.mis ...
- MySQL之高可用MHA部署
先说一下大概原理 虚拟机A ip为10.0.3.92 作为master 虚拟机B ip为10.0.3.102 作为slave1 虚拟机C ip为10.0.3.103 作为 ...
- 如何使用百度bae部署web项目
百度bae提供了支持各种开发环境的的应用引擎,包括node.js.php.java等,而且还免费提供了一定容量的mysql.mongodb.redis等数据库,所以,可以把它当作一个云服务器来使用.而 ...
- Python3:sorted()函数及列表中的sort()函数
一.sort,sorted函数介绍: Sort函数是list列表中的函数,而sorted可以对list或者iterator进行排序. 下面我们使用help来查看他们的用法及功能: sort: ...
- STL 的 vector 根据元素的值来删除元素的方法
vector 的 erase( ) 只能删除迭代器,所以要想删某种值的元素,需要这样: 假设有一个 vector 叫 vt ,则 vt.erase( remove( vt.begin() , vt.e ...
- jquery遍历table的tr获取td的值
方法一: var siginArray = []; $("#tbody").children("tr").each(function () { var sigi ...
- 无界面运行Jmeter压测脚本 --后知者
原文作者---后知者 原文地址:http://www.cnblogs.com/houzhizhe/p/8119735.html [后知者的故事]:针对单一接口压测时出现了从未遇到的问题,设好并发量后用 ...
- HanLP二元核心词典详细解析
本文分析:HanLP版本1.5.3中二元核心词典的存储与查找.当词典文件没有被缓存时,会从文本文件CoreNatureDictionary.ngram.txt中解析出来存储到TreeMap中,然后构造 ...