zoukankan      html  css  js  c++  java
  • [创建型模式] Singleton

    Singleton.h

    //
    //  Singleton.h
    //  Singleton
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #ifndef _SINGLETON_H_
    #define _SINGLETON_H_
    
    #include <iostream>
    using namespace std;
    
    class Singleton
    {
        public:
        static Singleton* Instance();
        
        protected:
        Singleton();
        
        private:
        static Singleton* _instance;
        
    };
    
    #endif  //~_SINGLETON_H_
    

    Singleton.cpp

    //
    //  Singleton.cpp
    //  Singleton
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #include "Singleton.h"
    
    #include <iostream>
    using namespace std;
    
    Singleton* Singleton::_instance = 0;
    
    Singleton::Singleton()
    {
        cout<<"Singleton..."<<endl;
    }
    
    Singleton* Singleton::Instance()
    {
        if(_instance==0){
            _instance = new Singleton();
        }
        
        return _instance;
    }
    

    main.cpp

    //
    //  main.cpp
    //  Singleton
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    #include "Singleton.h"
    
    #include <iostream>
    using namespace std;
    
    int main (int argc, const char * argv[]) {
    
        Singleton* sgn = Singleton::Instance();
        
        return 0;
    }
    

  • 相关阅读:
    Java 抽象类
    7.队列的链表实现
    6.队列的数组实现
    5.栈的链表实现
    4.栈的数组实现
    3.线性表-cursor
    2.线性表-Linked list
    1.线性表-Array
    hello world!
    boost 大小端转换
  • 原文地址:https://www.cnblogs.com/shenfei2031/p/1979306.html
Copyright © 2011-2022 走看看