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;
    }
    

  • 相关阅读:
    redis问题排查
    javassist介绍
    Idea创建父子工程
    sentry的配置
    Redis的基本操作以及info命令
    es~日期类型需要注意的
    jboss~静态文件路由和自定义日志
    java~RMI引起的log4j漏洞
    链路跟踪~对接阿里ARMS
    navicat~导出数据库密码
  • 原文地址:https://www.cnblogs.com/shenfei2031/p/1979306.html
Copyright © 2011-2022 走看看