首页 > 其他 > 详细

单例模式

时间:2015-04-27 12:41:37      阅读:97      评论:0      收藏:0      [点我收藏+]

   单例模式:确保一个类只有一个实例,并提供全局访问点。——《HEAD FIRST 设计模式》

  我的c++代码:

#ifndef DESIGN_SINGLETON_H_
#define DESIGN_SINGLETON_H_

#include <iostream>

class Singleton
{
private:
    Singleton(){}
public:
    static Singleton* GetInstance();

private:
    static Singleton* g_instance;
};

#endif // DESIGN_SINGLETON_H_
#include "singleton.h"

Singleton* Singleton::g_instance = new Singleton();

Singleton* Singleton::GetInstance()
{
    if (g_instance == 0)
    {
        std::cout << "singleton initing..." << std::endl;
        g_instance = new Singleton();
    }

    return g_instance;
}
       个人感悟:待留。

单例模式

原文:http://www.cnblogs.com/foolbread/p/4459741.html

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