c++ code:
#include "stdio.h"
class singleton
{
static singleton *instance;//不能在这里初始化
singleton(){}
~singleton(){}
public:
static singleton * getInstance()
{
if(instance == NULL)
instance = new singleton;
return instance ;
}
};
singleton* singleton::b = NULL; //静态成员初始化, 不初始化会出错
void main()
{
singleton *b = singleton::getInstance();
if(b == 0)
puts("ww");
}
java code:
public class singleton
{
private static singleton instance = new singleton ();
private singleton(){}
// java 中不存在~singleton(){}
public static singleton getInstance()
{
return instance;
}
};
另外的代码:
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include
#include
/********************************************************************
created: 2010/01/06
created: 6:1:2010 16:57
file base: singleton
file ext: h
author: lizp
purpose: singleton练习
*********************************************************************/
class Singleton
{
private:
Singleton()
{
cout<<"create singleton!" <
~Singleton(){
cout<<"destroy singleton" <
}
static Singleton *instance;
public:
static Singleton* getInstance()
{
if(instance == NULL)
instance = new Singleton();
return instance;
}
};
namespace auto_prt_singleton
{
class Singleton
{
protected:
Singleton()
{
cout<<"construct auto_prt_singleton"<
virtual ~Singleton()
{
cout<<"~~ construct auto_prt_singleton"<
public:
static Singleton* getInstance()
{
if(NULL == instance.get())
instance.reset(new Singleton());
return instance.get();
}
protected:
friend auto_ptr
static auto_ptr
};
auto_ptr
}
namespace auto_prt_template
{
#if 0
template
class Singleton
{
protected:
Singleton()
{
cout<<"construct auto_prt_template"<
virtual ~Singleton()
{
cout<<"~~ construct auto_prt_template"<
Singleton(const Singleton&){}
Singleton &operator=(const Singleton&){}
public:
static T* getInstance()
{
if(NULL == instance.get())
instance.reset(new T());
return instance.get();
}
protected:
static auto_ptr
};
#endif
class CResGuard;
template
class Singleton
{
protected:
Singleton()
{
cout<<"construct auto_prt_template"<
virtual ~Singleton()
{
cout<<"~~ construct auto_prt_template"<
Singleton(const Singleton&){}
Singleton &operator=(const Singleton&){}
public:
static T* getInstance()
{
if(NULL == instance.get())
{
CResGuard::CGuard gd(m_rg);
if(NULL == instance.get())
instance.reset(new T());
}
return instance.get();
}
protected:
static auto_ptr
static CResGuard m_rg;
};
template
auto_ptr
template
CResGuard Singleton
#define DECLARE_CLASS_SINGLETON(type) \
friend auto_ptr
friend Singleton
class CManage_service
{
public:
void doSomething()
{
cout<<"do something"<
private :
CManage_service()
{
cout<<"construct cmange_sevice"<
virtual ~CManage_service()
{
cout<<"~~~~construct cmange_sevice"<
DECLARE_CLASS_SINGLETON(CManage_service)
};
typedef Singleton
class CResGuard
{
public:
CResGuard(){m_lGrdCnd = 0; InitializeCriticalSection(&m_cs);}
~CResGuard(){DeleteCriticalSection(&m_cs);}
bool IsGuard()const {return (m_lGrdCnd>0);}
public:
class CGuard
{
public:
CGuard(CResGuard& rg):m_rg(rg){m_rg.Guard();}
~CGuard(){m_rg.unGuard();}
private:
CResGuard &m_rg;
};
private:
void Guard(){m_lGrdCnd++;EnterCriticalSection(&m_cs);}
void unGuard(){LeaveCriticalSection(&m_cs);m_lGrdCnd--;}
private:
friend class CResGuard::CGuard;
CRITICAL_SECTION m_cs;
long m_lGrdCnd;
};
}
Singleton* Singleton::instance;
#endif
使用代码:
// gof.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Handler.h"
#include "singleton.h"
int main(int argc, char* argv[])
{
/************************************************************************/
/* chain of responsibility */
/************************************************************************/
CHandler *ch1 = new CConcreteHandler1();
CHandler *ch2 = new CConcreteHandler2();
CHandler *ch3 = new CConcreteHandler3();
ch2->successor(ch1);
ch3->successor(ch2);
ch2->HandleRequest();
ch3->HandleRequest();
/************************************************************************/
/* singleton */
/************************************************************************/
Singleton *s = Singleton::getInstance();
auto_prt_singleton::Singleton *as = auto_prt_singleton::Singleton::getInstance();
auto_prt_template::CManage_service *ast = auto_prt_template::slMange::getInstance();
system("pause");
return 0;
}