// 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++代码实现的更多相关文章

  1. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

  2. 设计模式入门,命令模式,c++代码实现

    // test06.cpp : Defines the entry point for the console application.////设计模式第5章 命令模式#include "s ...

  3. 设计模式入门,工厂模式,c++代码实现

    // test04.cpp : Defines the entry point for the console application.////设计模式第4章 工厂模式#include "s ...

  4. 设计模式之单件模式(Singleton Pattern)

    一.单件模式是什么? 单件模式也被称为单例模式,它的作用说白了就是为了确保“该类的实例只有一个” 单件模式经常被用来管理资源敏感的对象,比如:数据库连接对象.注册表对象.线程池对象等等,这种对象如果同 ...

  5. php设计模式总结-单件模式

    一.单件模式 英文叫做sington.其他语言中有叫做单例模式,其实都是一样的道理.保证只会出现单个实例,所以是单例.翻译成单件,永远只会产生一件,呵呵. 还有翻译成单元素模式.其实关键是看这个英文比 ...

  6. 【设计模式】单件模式(Singleton)--各类单件模式的比较

    单件模式 确保一个类只有一个实例,并提供一个安全的访问点. 线程安全+延时初始化+高性能(使用:延时初始化占位符模式) ------测试----------- 线程安全+[非]延时初始化 线程安全+延 ...

  7. Head First设计模式 1 设计模式入门 策略模式 观察者模式

    关于基本的OOP特征: OOP的几大特征是抽象 继承 封装 多态. 我们把共同的部分抽象出来作为抽象类的存在,使用继承和接口来实现多态,然后私有的部分封装起来.一定程度上说,这些概念都是简单的设计模式 ...

  8. 设计模式:桥接模式及代码示例、桥接模式在jdbc中的体现、注意事项

    0.背景 加入一个手机分为多种款式,不同款式分为不同品牌.这些详细分类下分别进行操作. 如果传统做法,需要将手机,分为不同的子类,再继续分,基本属于一个庞大的多叉树,然后每个叶子节点进行相同名称.但是 ...

  9. php设计模式之桥接模式实例代码

    <?php header("Content-type:text/html;charset=utf-8"); abstract class msg{ protected $se ...

随机推荐

  1. Extjs在form展示后台单个对象的属性

    目的:写一个按钮事件,点击时弹出一个win窗体,里面镶嵌form表单,并且展示后台单个对象的属性 先来后台: public void find(){ String clientId = request ...

  2. oracle 创建临时表空间/表空间,用户及授权

    1:创建临时表空间 create temporary tablespace user_temp tempfile 'Q:\oracle\product\10.2.0\oradata\Test\xyrj ...

  3. UITextInputMode

    An instance of the UITextInputMode class represents the current text-input mode. You can use this ob ...

  4. ElasticSearch 因为磁盘空间不够引起的数据插入错误。(message [ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];]])

    ES升级到624测试从Kettle插入数据的时候,经常提示如下错误 message [ClusterBlockException[blocked by: [FORBIDDEN/12/index rea ...

  5. redis实现 msetex和 getdel命令

    1.redis本身不提供 msetex命令(批量增加key并设置过期时间) class RedisExtend { private static final Logger logger = Logge ...

  6. Locust源码目录结构及模块作用

    Locust源码目录结构及模块作用如下: 参考文章:https://blog.csdn.net/biheyu828/article/details/84031942

  7. CDH集群安装配置(七)--CDH组件的安装和配置

    1. Clouder Manger页面的配置 访问主节点IP:(cdh1)192.168.80.81:7180 默认用户名和密码:admin,admin 选择一个版本 选择集群的服务器(agent), ...

  8. Linux使用日志

    Linux使用日志 ----------------------------------------------------------------------------- SecureCRTPor ...

  9. btn按钮事件

    1.用Delegate 和 Event 来定义一个通用类来处理事件 (观察者模式) using System.Collections; using System.Collections.Generic ...

  10. c# xml序列化和反序列化。也就是xml的解析和反解析。

    用习惯了newTownSoft.json 的json反序列化.碰到xml是真的不习惯. 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名.然后直接ne ...