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 }
  • 相关阅读:
    js学习总结----案例之拖拽
    面向对象-数据属性
    Apply和call方法-扩充函数赖以生存的作用域
    JS中的function
    JS数组
    JS需要注意的细节和一些基础知识
    策略模式+简单工厂模式
    多态
    MVC3学习 八 Action和result过滤器及日志处理
    MVC3学习 七 JQuery方式和微软自带的AJAX请求
  • 原文地址:https://www.cnblogs.com/dracohan/p/3810851.html
Copyright © 2011-2022 走看看