C++ Design Patterns : Template + Factory Method

本文介绍了一种泛型工厂模式的实现方式,通过C++模板技术创建了一个泛型工厂类,能够根据不同类型的键值创建相应的对象实例。此模式简化了对象创建过程,提高了系统的扩展性和灵活性。

//////////////////////////////////////////////////////////////////////////
//  文件名称: Factory.h
//  文件描述: 泛形工厂(工厂模式)
//  版本    : 1.0
//  作者    : Mor
//  完成日期: 2006-11-07
//////////////////////////////////////////////////////////////////////////

#ifndef __N_MOR_FACTORY_H__
 #define __N_MOR_FACTORY_H__
#pragma warning(disable:4786)

#include <map>

namespace N_Mor{
//////////////////////////////////////////////////////////////////////////
//  类名    : CBase
//  类描述  : 泛形基类(工厂模式)
//////////////////////////////////////////////////////////////////////////

template<typename T_Base>
class CBase
{
public:
 CBase(){}
 virtual ~CBase(){}

public:
 virtual T_Base* Create() const = 0;
};

//////////////////////////////////////////////////////////////////////////
//  类名    : CDerived
//  类描述  : 泛形派生类(工厂模式)
//////////////////////////////////////////////////////////////////////////

template<typename T_Base,typename T_Derived>
class CDerived : public CBase<T_Base>
{
public:
 CDerived(){}
 virtual ~CDerived(){}

public:
 virtual T_Base* Create() const {return(new T_Derived);}
};

//////////////////////////////////////////////////////////////////////////
//  类名    : CFactory
//  类描述  : 泛形工厂(工厂模式)
//////////////////////////////////////////////////////////////////////////

template<typename T_Key, typename T_Base>
class CFactory
{
protected:
 CFactory(){}
 ~CFactory()
 {
  std::map<T_Key,const CBase<T_Base> *>::iterator Iter;
  for( Iter = m_Creater.begin() ; Iter != m_Creater.end() ; ++Iter )
  {
   delete Iter->second;
  }
  m_Creater.clear();
 }

public:
 template<typename T_Derived>
  static bool Insert( const T_Key& Key )
 {
  std::map<T_Key,const CBase<T_Base> *>::iterator Iter = m_Creater.find(Key);
  if( Iter == m_Creater.end() )
  {
   m_Creater[Key] = new CDerived<T_Base,T_Derived>();
   return(true);
  }
  return(false);
 }

 template<typename T_Derived>
  static bool Insert( const T_Key& Key, const T_Derived& Derived )
 {
  return(Insert<T_Derived>(Key));
 }

 static bool Remove( const T_Key& Key )
 {
  std::map<T_Key,const CBase<T_Base> *>::iterator Iter = m_Creater.find(Key);
  if( Iter != m_Creater.end() )
  {
   delete Iter->second;
   m_Creater.erase(Iter);
   return(true);
  }
  return(false);
 }

 static T_Base* Return( const T_Key& Key )
 {
  std::map<T_Key,const CBase<T_Base> *>::iterator Iter = m_Creater.find(Key);
  return( Iter != m_Creater.end() ? Iter->second->Create() : NULL );
 }

private:
 static std::map<T_Key,const CBase<T_Base> *> m_Creater;

};

template<typename T_Key, typename T_Base>
 std::map<T_Key,const CBase<T_Base> *> CFactory<T_Key,T_Base>::m_Creater;

}//namespace N_Mor
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值