设计模式——单例模式(C++)
一: 饿汉式单例:
静态区初始化instance,然后通过getInstance返回。这种方式没有多线程的问题,是一种以空间换时间的方式,不管程序用不用,都会构造唯一的实例。
#pragma once
#include <Windows.h>
#include "Lock.h"
class Singleton
{
private:
Singleton(); // 构造函数只能在这个类内部自己能用,创建唯一实例
Singleton(const Singleton&); // 防止被复制
Singleton& operator=(const Singleton&); //防止赋值
~Singleton();
static Singleton* m_pInstance;
public:
static Singleton* getInstance()
{
return m_pInstance;
}
};
#include "stdafx.h"
#include "Singleton.h"
Singleton::Singleton()
{
}
Singleton::~Singleton()
{
}
Singleton::Singleton(const Singleton&)
{}
Singleton& Singleton::operator=(const Singleton&)
{
return *this;
}
Singleton* Singleton::m_pInstance = new Singleton();
// DesignPattern.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace std;
int main()
{
Singleton *aaa = Singleton::getInstance();
Singleton *bbb = Singleton::getInstance();
cout <<(int*)aaa << endl;
cout <<(int*) bbb << endl;
}
二: 懒汉式单例:
在需要的时候才创建唯一的实例,可能有多线程的问题。下面是采用双重检查的方式实现:
2.1 用类的静态成员的方式
#include <Windows.h>
#include "Lock.h"
class Singleton
{
private:
Singleton(); // 构造函数只能在这个类内部自己能用,创建唯一实例
Singleton(const Singleton&); // 防止被复制
Singleton& operator=(const Singleton&); // 防止赋值
~Singleton();
static Singleton* m_pInstance;
public:
static Singleton* getInstance()
{
if (m_pInstance == NULL)
{
lock(); // 使用其他的方式实现,这里只是意思一下
if (m_pInstance == NULL)
{
m_pInstance = new Singleton();
}
unlock();
}
return m_pInstance;
}
};
#include "stdafx.h"
#include "Singleton.h"
Singleton::Singleton()
{
}
Singleton::~Singleton()
{
}
Singleton::Singleton(const Singleton&)
{}
Singleton& Singleton::operator=(const Singleton&)
{
return *this;
}
Singleton* Singleton::m_pInstance =NULL;
// DesignPattern.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace std;
int main()
{
Singleton *aaa = Singleton::getInstance();
Singleton *bbb = Singleton::getInstance();
cout <<(int*)aaa << endl;
cout <<(int*) bbb << endl;
}
2.1 用局部静态变量的方式
#pragma once
#include <Windows.h>
#include "Lock.h"
class Singleton
{
private:
Singleton(); // 构造函数只能在这个类内部自己能用,创建唯一实例
Singleton(const Singleton&); // 防止被复制
Singleton& operator=(const Singleton&); // 防止赋值
~Singleton();
public:
static Singleton* getInstance()
{
static Singleton singleton;
// C++0X 以后,局部静态变量可以保证线程安全的;但是C++0X之前,仍然要加锁
return &singleton;
}
};
#include "stdafx.h"
#include "Singleton.h"
Singleton::Singleton()
{
}
Singleton::~Singleton()
{
}
Singleton::Singleton(const Singleton&)
{}
Singleton& Singleton::operator=(const Singleton&)
{
return *this;
}
// DesignPattern.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace std;
int main()
{
Singleton *aaa = Singleton::getInstance();
Singleton *bbb = Singleton::getInstance();
cout <<(int*)aaa << endl;
cout <<(int*) bbb << endl;
}
设计模式——单例模式(C++)的更多相关文章
- 设计模式 单例模式(Singleton) [ 转载2 ]
设计模式 单例模式(Singleton) [ 转载2 ] @author java_my_life 单例模式的结构 单例模式的特点: 单例类只能有一个实例. 单例类必须自己创建自己的唯一实例. 单例类 ...
- 设计模式 单例模式(Singleton) [ 转载 ]
设计模式 单例模式(Singleton) [ 转载 ] 转载请注明出处:http://cantellow.iteye.com/blog/838473 前言 懒汉:调用时才创建对象 饿汉:类初始化时就创 ...
- c#设计模式-单例模式(面试题)
c#设计模式-单例模式 单例模式三种写法: 第一种最简单,但没有考虑线程安全,在多线程时可能会出问题, public class Singleton { private static Singleto ...
- java设计模式单例模式 ----懒汉式与饿汉式的区别
常用的五种单例模式实现方式 ——主要: 1.饿汉式(线程安全,调用率高,但是,不能延迟加载.) 2.懒汉式(线程安全,调用效率不高,可以延时加载.) ——其他: 1.双重检测锁式(由于JVM底层内部模 ...
- 最简单的设计模式——单例模式的演进和推荐写法(Java 版)
前言 如下是之前总结的 C++ 版的:软件开发常用设计模式—单例模式总结(c++版),对比发现 Java 实现的单例模式和 C++ 的在线程安全上还是有些区别的. 概念不多说,没意思,我自己总结就是: ...
- ES6教程-字符串,函数的参数,了解函数的arguments对象,js面向对象,设计模式-单例模式,解构赋值
前言 主要讲解了ES6对字符串的拓展,包括includes,startsWith和endsWith,另外增加了字符串模板. Start includes()是否包含 startsWith()以什么开头 ...
- Java设计模式の单例模式
-------------------------------------------------- 目录 1.定义 2.常见的集中单例实现 a.饿汉式,线程安全 但效率比较低 b.单例模式的实现:饱 ...
- java设计模式——单例模式(一)
一. 定义与类型 定义:保证一个类仅有一个实例,并提供一个全局访问点 类型:创建型 二. 适用场景 想确保任何情况下都绝对只用一个实例 三. 优缺点 优点: 在内存里只有一个实例,减少了内存开销 可以 ...
- php实现设计模式————单例模式
php实现设计模式————单例模式 什么是单例模式 为什么要使用单例模式 php中有哪些方式实现新建一个对象实例 如何阻止这种实例化实现理想的单例模式 代码实现 什么是单例模式 为什么要使用单例模式 ...
- JAVA设计模式-单例模式(Singleton)线程安全与效率
一,前言 单例模式详细大家都已经非常熟悉了,在文章单例模式的八种写法比较中,对单例模式的概念以及使用场景都做了很不错的说明.请在阅读本文之前,阅读一下这篇文章,因为本文就是按照这篇文章中的八种单例模式 ...
随机推荐
- Confluence 安装
一.事前准备 1.jdk安装:5.8.10的jdk至少是7,其中7中还有很多官网是不建议的,这儿选中jdk-7u79 二.安装Confluence 双击atlassian-confluence-5.8 ...
- git clone 提示输入git@xxx的密码
如下: suse:~/ecox # git clone git@vcs.in.ww-it.cn:ecox/ecox.git 正克隆到 'ecox'... git@vcs.in.ww-it.cn's p ...
- easyUI---分页插件
设置 //分页组件 $('#detailLayer .detailPag').pagination({ pageNumber: 1, pageSize: 10, total: result.Total ...
- history.go(-1)在不同浏览器中的解析
今天遇到个问题: <a href="#" onclick="history.go(-1)">后退</a> 点击"后退" ...
- 为什么要重写toString()方法和hashcode()方法
一.toString(): 在Object类里面定义toString()方法的时候返回的对象的哈希code码,这个hashcode码不能简单明了的表示出对象的属性.所以要重写toString()方法. ...
- learn go ifelse
package main // 参考文档: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/05.1.md im ...
- 博客迁移至“零一积流|it-refer.com”
虽然在博客园写了没几篇文章,考虑到个人发展,自己还是建立一个独立的站点:零一积流. 以后直接在自己网站上写东西了,此处只用做文章收藏.
- 二次剩余-Cipolla
二次剩余 \(Cipolla\) 算法 概述 大概就是在模 \(p\) 意义下开根号,如求解方程\(x^2\equiv n(mod\ p)\). 这里只考虑 \(p\) 为素数的情况.若 \(p=2\ ...
- 通过解读 WPF 触摸源码,分析 WPF 插拔设备触摸失效的问题(问题篇)
在 .NET Framework 4.7 以前,WPF 程序的触摸处理是基于操作系统组件但又自成一套的,这其实也为其各种各样的触摸失效问题埋下了伏笔.再加上它出现得比较早,触摸失效问题也变得更加难以解 ...
- 如何在 .NET 库的代码中判断当前程序运行在 Debug 下还是 Release 下
我们经常会使用条件编译符 #if DEBUG 在 Debug 下执行某些特殊代码.但是一旦我们把代码打包成 dll,然后发布给其他小伙伴使用的时候,这样的判断就失效了,因为发布的库是 Release ...