设计模式入门,单件模式,c++代码实现
// test05.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//设计模式第5章 单件模式
class Singleton
{
private:
static Singleton* uniqueInstance;
private:
Singleton(){}
public:
static synchronized Singleton* getInstance()//synchronized在java中表示线程同步,多线程保护
{
if (uniqueInstance == NULL)
{
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
};
class Singleton1
{
private:
static Singleton1* uniqueInstance = new Singleton1();
private:
Singleton1(){}
public:
static Singleton1* getInstance()
{
return uniqueInstance;
}
};
class Singleton2
{
private:
volatile static Singleton2* uniqueInstance;
private:
Singleton2(){}
public:
volatile static Singleton2* getInstance()
{
if (uniqueInstance == NULL)
{
synchronized(Singleton2.class)//java中的锁
{
if (uniqueInstance == NULL)
{
uniqueInstance = new Singleton2();
}
}
}
return uniqueInstance;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
设计模式入门,单件模式,c++代码实现的更多相关文章
- 设计模式入门,策略模式,c++代码实现
// test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...
- 设计模式入门,命令模式,c++代码实现
// test06.cpp : Defines the entry point for the console application.////设计模式第5章 命令模式#include "s ...
- 设计模式入门,工厂模式,c++代码实现
// test04.cpp : Defines the entry point for the console application.////设计模式第4章 工厂模式#include "s ...
- 设计模式之单件模式(Singleton Pattern)
一.单件模式是什么? 单件模式也被称为单例模式,它的作用说白了就是为了确保“该类的实例只有一个” 单件模式经常被用来管理资源敏感的对象,比如:数据库连接对象.注册表对象.线程池对象等等,这种对象如果同 ...
- php设计模式总结-单件模式
一.单件模式 英文叫做sington.其他语言中有叫做单例模式,其实都是一样的道理.保证只会出现单个实例,所以是单例.翻译成单件,永远只会产生一件,呵呵. 还有翻译成单元素模式.其实关键是看这个英文比 ...
- 【设计模式】单件模式(Singleton)--各类单件模式的比较
单件模式 确保一个类只有一个实例,并提供一个安全的访问点. 线程安全+延时初始化+高性能(使用:延时初始化占位符模式) ------测试----------- 线程安全+[非]延时初始化 线程安全+延 ...
- Head First设计模式 1 设计模式入门 策略模式 观察者模式
关于基本的OOP特征: OOP的几大特征是抽象 继承 封装 多态. 我们把共同的部分抽象出来作为抽象类的存在,使用继承和接口来实现多态,然后私有的部分封装起来.一定程度上说,这些概念都是简单的设计模式 ...
- 设计模式:桥接模式及代码示例、桥接模式在jdbc中的体现、注意事项
0.背景 加入一个手机分为多种款式,不同款式分为不同品牌.这些详细分类下分别进行操作. 如果传统做法,需要将手机,分为不同的子类,再继续分,基本属于一个庞大的多叉树,然后每个叶子节点进行相同名称.但是 ...
- php设计模式之桥接模式实例代码
<?php header("Content-type:text/html;charset=utf-8"); abstract class msg{ protected $se ...
随机推荐
- django数据模型中关于on_delete的使用
django数据模型中关于on_delete的使用 class BookModel(models.Model): """ 书籍表 """ b ...
- HTML中特殊字符
HTML中的字符详解 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 ! ! " " " # # $ $ % % & & & ' ...
- ARC初步介绍
[转载自 http://onevcat.com/2012/06/arc-hand-by-hand/] 手把手教你ARC——iOS/Mac开发ARC入门和使用 Revolution of Objecti ...
- 可持久化数据结构QwQ(持续更新中)
可持久化留下的迹象 我们俯身欣赏 ——<膜你抄>By Menci&24OI Micardi最近在学可持久化的东西,可持久化线段树.可持久化并查集.可持久化01Trie......等 ...
- POJ3666 Making the Grade
POJ3666 Making the Grade 题意: 给定一个长度为n的序列A,构造一个长度为n的序列B,满足b非严格单调,并且最小化S=∑i=1N |Ai-Bi|,求出这个最小值S,1<= ...
- luogu5282 【模板】快速阶乘算法
由于巨佬 shadowice1984 卡时限,本代码已经 T 请不要粘上去交 退役之后再写一个常数小的多项式取模吧 一句话题意:NP问题,求N!%P 吐槽:出题人太毒瘤...必须写任意模数NTT,而且 ...
- sap server笔记
system 就是sap hana database,如果一个system有多个instance,则必须分散到不同的host中,每个system有唯一的sid. hello各位,jackie建议我们去 ...
- Maven 远程仓库下载慢的的解决方案
配置很简单,修改conf文件夹下的settings.xml文件,添加如下镜像配置: 我直接去设置maven目录下面的setttings文件 添加镜像站点 <mirrors> <mir ...
- USART列子
#include "stm32f10x.h" void USART_INit(void) { GPIO_InitTypeDef GPIO_Initstructe; USART_In ...
- SELinux 引起的 Docker 启动失败
问题描述 Linux OS 版本 CentOS Linux release 7.2.1511 (Core) 启动Docker service docker start 启动失败信息 原因分析 Erro ...