1 public class SingleObj
 2 {
 3     private static SingleObj OnlyOne = null;
 4     private SingleObj()
 5     {}
 6     public static SingleObj getInstance()
 7     {
 8         
 9         if(OnlyOne = null) OnlyOne = new SingleObj();
10         return OnlyOne;
11         
12     }
13 }
posted on 2007-02-08 09:23 -274°C 阅读(508) 评论(1)  编辑  收藏 所属分类: 计算机综合


FeedBack:
# re: 单态设计模式
2008-04-22 21:43 | -274°C
C++ 写了个单体模式:

#include "stdafx.h"

#include <iostream>
#include <string>


using namespace std;

class Logger
{
public:
static Logger* getInstance();
string name ;
protected:
Logger(){}

private:
static Logger* _instance;
};

Logger* Logger::_instance = 0;
Logger* Logger::getInstance()
{
if (_instance == 0)
{
_instance = new Logger;
}
return _instance;
}
//测试代码
int _tmain(int argc,_TCHAR* argv[])
{
Logger* log = Logger::getInstance();
log->name = "test~~~~~~~~~~~~~~~~~~~~~";

cout << "编译通过" << endl;
cout << log->name<< endl;
return 0;
}  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909109
  • 排名 - 40

最新评论