首页 > 其他 > 详细

【设计模式】Singleton Pattern

时间:2015-06-13 18:24:29      阅读:242      评论:0      收藏:0      [点我收藏+]

main.cpp

#include "Singleton.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    
    Singleton *sgn = Singleton::Instance();

    return 0;
}

Singleton.h

#ifndef _SINGLETON_H
#define _SINGLETON_H

class Singleton {
public :
    static Singleton *Instance();
protected :
    Singleton();
private :
    static Singleton *_instance;
};

#endif

Singleton.cpp

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

【设计模式】Singleton Pattern

原文:http://www.cnblogs.com/Susake/p/4573764.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!