码迷,mamicode.com
首页 > 其他好文 > 详细

【设计模式】Singleton Pattern

时间:2015-06-13 18:27:35      阅读:141      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!