设计模式-单例模式

 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 }

 

posted on 2014-06-26 21:02  醉清风JM  阅读(147)  评论(0编辑  收藏  举报

导航