// 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. bzoj2705Longge的问题

    题目链接 题意很简单 $$ans=\sum_{i=1}^{n}gcd(i,n)$$ 然后推一下式子,求一下欧拉函数就好了 细节是由于$BZOJ$的评测计时策略, 不能线性筛啊$……$ 必须每个数单独筛 ...

  2. ArchLinux 下 OpenSSH 高级运用

    00x0.相关介绍 OpenSSH(OpenBSD Secure Shell)使用 SSH 通过计算机网络加密通信的实现. 它是替换由 SSH Communications Security 所提供的 ...

  3. jquery事件二 -- 选项卡,失去焦点

    以之前的选项卡例子为原版,当选上某一个选项卡的时候,选项卡周围会有一个蓝色的边框影响视觉体验,那么应该怎么去掉这个边框色呢?只需要加一行blur()--失去焦点函数就可以了 <!DOCTYPE ...

  4. 公司git流程图,广告业务术语

  5. mysq exists和in

    我们在学习Yii2的时候,一定接触过这样的where输入 $query->where(["exists",xxxx]); User::find()->where([&q ...

  6. Maximum call stack size exceeded

    写vue时报了如下错误 Maximum call stack size exceeded 栈溢出,因为在调用函数时使用了递归调用,而且没有写跳出条件,导致了该错误

  7. Drupal Coder 模块远程命令执行分析(SA-CONTRIB-2016-039)

    转载请注明文章出处:http://www.cnblogs.com/magic-zero/p/5787181.html 起初看到这个漏洞的时候是在exploit-db上边.地址在这里:https://w ...

  8. js计算数值

    1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...

  9. apk包不能生成的原因之debug.keystore

    在Eclipse里面编译生成的APK中有一个签名的,它默认的key是debug.keystore,它默认的路径是: C:\Users\<用户名>\.android\debug.keysto ...

  10. Jmeter实例

    我们在性能测试过程中,首先应该去设计测试场景,模拟真实业务发生的情境,然后针对这些场景去设计测试脚本.为了暴露出性能问题,要尽可能的去模拟被测对象可能存在瓶颈的测试场景. 我在本地部署了一个项目,可以 ...