设计模式入门,单件模式,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 ...
随机推荐
- jquery源码解析:pushStack,end,ready,eq详解
上一篇主要讲解了jQuery原型中最重要的方法init.接下来再讲一些比较常用的原型方法和属性 core_slice = [].slice, jQuery.fn = jQuery.prototype ...
- java操作AWS S3一些坑记录
1,aws sdk jar版本不一致问题 一开始我在pom.xml中只配置了如下aws-java-sdk-s3 <!-- https://mvnrepository.com/artifact/c ...
- ubuntu14.04安装zabbix
1. apt-get updateapt-get install apache2 mysql-server libapache2-mod-php5 php5-gd php5-mysql php5-co ...
- 微信小程序之WebSocket
本文版权归 OSChina jsongo0 所有,转载请标明出处,以示尊重! 原文:https://my.oschina.net/jsongo/blog/757871 为什么需要websocket?传 ...
- Ping程序
一.概述 Ping程序是对两个TCP/IP系统连通性进行测试的基本工具.该程序发送一份ICMP回显请求报文给主机,并等待返回ICMP回显应答. 二.格式 大多数TCP/IP实现都在内核中直接支持Pin ...
- 关于在iOS应用中跳转到AppStore
1.获取app 在AppStore上的网址 eg: NSString * appURLStr = @"https://itunes.apple.com/cn/app/shi-ke-zu-qi ...
- 可变参数中size_t遇见的问题
在修改php扩展Trie时,出现了一个小bug PHP_FUNCTION(trie_filter_load) { Trie *trie; char *path; int path_len; if (z ...
- php获取随机字符串的几种方法
方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...
- c语言-猜生日算法
#include<stdio.h>int main(){ int a1[6]={1,3,5,7,9,11}; int a2[6]={2,3,6,7,10,11}; int a3[6]={4 ...
- 1、Caffe数据层及参数
要运行Caffe,需要先创建一个模型(model),每个模型由许多个层(layer)组成,每个层又都有自己的参数, 而网络模型和参数配置的文件分别是:caffe.prototxt,caffe.solv ...