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 }
  • 相关阅读:
    转载:AAC编解码概述
    转载:ADTS header
    wcf寄宿在iis上的跨域访问问题【不止是添加跨域文件】
    转 http 分析工具
    时间管理1
    关于silverlight和Wcf分布式部署注意问题(收藏夹)
    c#修改xml文件
    关于在线编辑的异常
    创业文摘5--从程序员转向企业家的10个建议
    silverlight 后台代码生成gridview
  • 原文地址:https://www.cnblogs.com/dracohan/p/3810851.html
Copyright © 2011-2022 走看看