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,所以在方 ...
随机推荐
- break与continue关键字的使用
break与continue关键字的使用break:使用在switch-case中或者循环中 如果使用在循环中,表示:结束当前循环 public class V{ public static void ...
- hdu1796 How many integers can you find 容斥原理
Now you get a number N, and a M-integers set, you should find out how many integers which are small ...
- day 57 jQuery的补充
.data() 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. .data(key, value): 描述:在匹配的元素上存储任意相关数据. ...
- pgbench 安装试用
pgbench 是一个方便的pg 性能测试工具,以下是简单的测试试用 安装 安装pg yum install https://download.postgresql.org/pub/repos/yum ...
- AI 学习
极简状态机: /* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using UnityEngine; using System.Collections; namespace Voi ...
- 互换元素(swap,swap_ranges)
swap template <class Assignable> void swap(Assignable &a,Assignable &b); iter_swap tem ...
- Distributed processing
Distributed processing Tool 好处 坏处 类型 支持序列化 支持根据负载动态调度任务 支持c 支持dependency的调度 有成熟的library Actor model ...
- create-react-app-typescript 知识点
github:https://github.com/wmonk/create-react-app-typescript 报错:Import sources within a group must be ...
- secureCRT 设置证书免密登陆
1 第一步 2 第二步 3 第三步 4 第4 步 ,然后选择你的 私钥文件
- js 控件样式显隐和赋值
控制样式显示: $("#againMetting")[0].style.display = "inline";//显示 $("#againMettin ...