知了博客

集天地之精华,吸日月之灵气

« 工作总结(2010-01)虚析构函数 »

singleton模式

singleton模式,些模式变体有几种,这种基本实现是在单线程中实现的
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" < if(instance!=NULL)delete instance;
}
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 instance;
};
auto_ptr Singleton::instance;
}
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 instance;
};
#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 instance;
static CResGuard m_rg;
};
template
auto_ptr Singleton::instance;
template
CResGuard Singleton::m_rg;

#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 slMange;
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;
}
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619 Code detection by Codefense  theme by BokeZhuti

Copyright know blog. Some Rights Reserved.站长(msn):webmaster#webgou.info(#换成@) 粤ICP备09183716号