zoukankan      html  css  js  c++  java
  • 设计模式-单例模式

     1 ///////////////////////////////////////////////////////////////////////////////
     2 //
     3 //  FileName    :   singleton.h
     4 //  Version     :   0.10
     5 //  Author      :   Jimmy Han
     6 //  Date        :   2014/6/25 21:54
     7 //  Comment     :   
     8 //
     9 ///////////////////////////////////////////////////////////////////////////////
    10 
    11 #ifndef __H__
    12 #define __H__
    13 #include <iostream>
    14 using namespace std;
    15 
    16 class Singleton {
    17 public:
    18     static Singleton* getInstance(){
    19             if(uniqueInstance == NULL){
    20                 cout << "synchronized!" << endl;
    21                 if(uniqueInstance == NULL)
    22                     uniqueInstance = new Singleton();
    23             }
    24             return uniqueInstance;
    25         }
    26 private: 
    27         static Singleton* uniqueInstance;
    28         
    29         Singleton();
    30 };
    31 
    32 
    33 #endif
     1 ///////////////////////////////////////////////////////////////////////////////
     2 //
     3 //  FileName    :   singleton.cpp
     4 //  Version     :   0.10
     5 //  Author      :   Jimmy Han
     6 //  Date        :   2014/6/25 21:54
     7 //  Comment     :  
     8 //
     9 ///////////////////////////////////////////////////////////////////////////////
    10 
    11 #include "singleton.h"
    12 #include <iostream>
    13 using namespace std;
    14 
    15 Singleton* Singleton::uniqueInstance = NULL;
    16 
    17 Singleton::Singleton()
    18 {
    19     cout << "Singleton class was initilized!" << endl;
    20 }
    21 
    22     
    23     
     1 ///////////////////////////////////////////////////////////////////////////////
     2 //
     3 //  FileName    :   singleton_client.cpp
     4 //  Version     :   0.10
     5 //  Author      :   Jimmy Han
     6 //  Date        :   2014/6/25 21:54
     7 //  Comment     :  
     8 //
     9 ///////////////////////////////////////////////////////////////////////////////
    10   
    11 #include "singleton.h"
    12 #include <iostream>
    13 using namespace std;
    14 
    15 int main()
    16 {
    17     Singleton* handle = Singleton::getInstance();
    18 
    19     return 0;    
    20 }
  • 相关阅读:
    QTP自学攻略
    自动检查页面链接是否有效
    使用Loadrunner进行接口测试
    缺陷管理方案
    python读取文本、配对、插入数据脚本
    QTP 中对象操作
    python学习笔记(三)--条件语句
    npm WARN uninstall not installed in /Users/hrt0kmt/node_modules: "xxx"
    appium mac 下 安装及踩坑
    homebrew -v 或homebrew -doctor报错请检查 .bash_profile是否有误
  • 原文地址:https://www.cnblogs.com/dracohan/p/3810851.html
Copyright © 2011-2022 走看看