最新文章
热门文章
WinPE操作系统的使用及简介(破解 
QQ象棋,联众象棋游戏如何作弊 
如何解决网页中图片大小类型等属 
总结几种结构体初始化方法 
关于硕士毕业论文自动生成目录和 
VC建立类向导(class wizard)错误 
C++中取随机数函数rand和srand用 
VC编写自己构造http协议数据的po 
如何查看得到windows系统管理员帐 
由DCOM权限引起的在windows2003上 
当前位置:李露的博客 >> 电脑技术 >> 浏览文章
三种单例模式动态、静态和可配置
更新日期:2010年09月26日  来源:本站原创  作者:天漏客   访问次数:次  【字体:

#include "stdafx.h"
#include <iostream>
using namespace std;

#define SAFE_DELETE(ptr) if(ptr){delete (ptr); (ptr) = NULL;}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//动态的
template< class TChild >
class SingletonDynamic
{
public:
 static TChild* instance()
 {
  return GetInstance();
 }

 static void Release()
 {
  // 可以取到
  TChild* &pChild = GetInstanceObj();

  // 释放
  SAFE_DELETE( pChild );

  // 设置释放成功
  SetReleased();
 }

protected:
 static TChild* GetInstance()
 {
  if( IsReleased() ) return NULL;

  return GetInstanceObj();
 }

 static TChild*& GetInstanceObj()
 {
  // 相当于成员变量
  static TChild* s_pChild = NULL;
  if( NULL == s_pChild )
   s_pChild = new TChild();

  return s_pChild;
 }

static bool  IsReleased()
{
 return GetReleaseObj();
}

static void  SetReleased()
{
 bool &IsReleased = GetReleaseObj();
 IsReleased = true;
}

static bool&  GetReleaseObj()
{
 // 相当于成员变量
 static bool IsReleased = false;
 return IsReleased;
}
};


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/**
@brief  静态单例
@remarks 可优化
*/
template< class TChild >
class SingletonStatic
{
public:
 static TChild* instance()
 {
  static TChild s_Child;
  return &s_Child;
 }
};

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//接口可配置单例
template< class Type >
class SingletonConfigurable
{
public:
 static bool   Available();
 static Type *   GetInstance();

protected:
 void     install();
 void     uninstall();

private:
 static Type *   m_pInstance;

};

template< class Type >
Type *SingletonConfigurable<Type>::m_pInstance = NULL;

template< class Type >
inline bool SingletonConfigurable<Type>::Available()
{
 if ( m_pInstance != NULL )
 {
  return TRUE;
 }

 return FALSE;
}

template< class Type >
inline Type *SingletonConfigurable<Type>::GetInstance()
{
 assert( m_pInstance != NULL );

 return m_pInstance;
}

template< class Type >
inline void SingletonConfigurable<Type>::install()
{
 assert( m_pInstance == NULL );

 m_pInstance = static_cast<Type *>( this );
}

template< class Type >
inline void SingletonConfigurable<Type>::uninstall()
{
 assert( m_pInstance != NULL );

 m_pInstance = NULL;
}
//全局变量
int gCount = 0;
//测试类
class Test
{
public:
 Test()
 {
  gCount++;
 }
 void OutPut()
 {
  cout << gCount << endl;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{

 Test *pTest = SingletonStatic<Test>::instance();
 
 Test *pTest2 = SingletonStatic<Test>::instance();
 pTest->OutPut();
 
 return 0;
}

 

发表评论】【告诉好友】【打印此文】【收藏此文】【关闭窗口
上一篇:自己整理的windows路径操作API函数 下一篇:[源码收藏]MFC写的系统托盘图标,有泡泡提示。

Copyright 2006-2012 Powered by LiLu.NAME,李露的博客 All Rights Reserved.
E-Mail:lilu.name#gamil.com(注意是gmail,自己改) QQ:285252760
苏ICP备08016526号