头文件:
/*
整合模版,使得类的注册和创建可以使用字符串,这样能够用于多语言编程环境(脚本引擎在底层的对象创建(直接使用字符串创建,而无需再做内部解析))。
*
*这里的延迟创建是利用了函数创建对象的函数指针,将子类通过模版参数传入
*/
#pragma once
#pragma warning (disable:4786)
/* *******************************************************************
created: 2013-1-29
author: mark
filename: AbstractFactory.h
purpose: a generic factory method lib
Copyright (C) 2008 - All Rights Reserved
******************************************************************** */
#include < string>
#include <map>
namespace lib
{
// 通用工厂类,根据传入的id创建产品对象
template <typename BaseType, typename KeyType=std:: string>
class factory
{
private:
typedef std::auto_ptr<BaseType> (*BaseCreateFunc)();
typedef typename std::map<KeyType, BaseCreateFunc> FuncRegistry;
public:
static factory<BaseType, KeyType>& get_instance(){ // static singleton,不适用于多线程
static factory<BaseType, KeyType> obj;
return obj;
}
std::auto_ptr<BaseType> create( const KeyType& id) const{
std::auto_ptr<BaseType> obj;
// 这句话真伤脑筋,还是用C++11的auto关键字吧。
typename FuncRegistry::const_iterator regEntry = _registry.find(id);
// auto regEntry = _registry.find(id);
if (regEntry != _registry.end()) {
obj = regEntry->second();
}
return obj;
}
void _register_create_function( const KeyType& id, BaseCreateFunc func){_registry[id] = func;}
private:
factory( void){}
factory( const factory& other);
factory operator=( const factory& other);
private:
FuncRegistry _registry;
};
// 类型(DerivedType)注册类,只要在DerivedType类定义(DerivedType.cpp文件)的最后声明一个 lib::factory_register<Base, Derived> reg(id);对象即可
template <typename BaseType, typename DerivedType, typename KeyType=std:: string>
class factory_register
{
public:
factory_register( const KeyType& id){
factory<BaseType, KeyType>::get_instance()._register_create_function(id, _create_instance);
}
private:
static std::auto_ptr<BaseType> _create_instance(){ return std::auto_ptr<BaseType>( new DerivedType);}
private:
friend class factory<BaseType, KeyType>;
};
}
测试用例:
*
*这里的延迟创建是利用了函数创建对象的函数指针,将子类通过模版参数传入
*/
#pragma once
#pragma warning (disable:4786)
/* *******************************************************************
created: 2013-1-29
author: mark
filename: AbstractFactory.h
purpose: a generic factory method lib
Copyright (C) 2008 - All Rights Reserved
******************************************************************** */
#include < string>
#include <map>
namespace lib
{
// 通用工厂类,根据传入的id创建产品对象
template <typename BaseType, typename KeyType=std:: string>
class factory
{
private:
typedef std::auto_ptr<BaseType> (*BaseCreateFunc)();
typedef typename std::map<KeyType, BaseCreateFunc> FuncRegistry;
public:
static factory<BaseType, KeyType>& get_instance(){ // static singleton,不适用于多线程
static factory<BaseType, KeyType> obj;
return obj;
}
std::auto_ptr<BaseType> create( const KeyType& id) const{
std::auto_ptr<BaseType> obj;
// 这句话真伤脑筋,还是用C++11的auto关键字吧。
typename FuncRegistry::const_iterator regEntry = _registry.find(id);
// auto regEntry = _registry.find(id);
if (regEntry != _registry.end()) {
obj = regEntry->second();
}
return obj;
}
void _register_create_function( const KeyType& id, BaseCreateFunc func){_registry[id] = func;}
private:
factory( void){}
factory( const factory& other);
factory operator=( const factory& other);
private:
FuncRegistry _registry;
};
// 类型(DerivedType)注册类,只要在DerivedType类定义(DerivedType.cpp文件)的最后声明一个 lib::factory_register<Base, Derived> reg(id);对象即可
template <typename BaseType, typename DerivedType, typename KeyType=std:: string>
class factory_register
{
public:
factory_register( const KeyType& id){
factory<BaseType, KeyType>::get_instance()._register_create_function(id, _create_instance);
}
private:
static std::auto_ptr<BaseType> _create_instance(){ return std::auto_ptr<BaseType>( new DerivedType);}
private:
friend class factory<BaseType, KeyType>;
};
}
#include <iostream>
#include "AbstractFactory.h"
// 基类
class Base
{
public:
virtual void print( void){
std::cout<<"base class"<<std::endl;
}
};
lib::factory_register<Base, Base> reg1("base"); // 注册Base类型,定义一个file scope的变量,通常应该置于Base.cpp文件结尾
// 派生类
class Derived: public Base
{
public:
virtual void print( void){
std::cout<<"derived class"<<std::endl;
}
};
lib::factory_register<Base, Derived> reg2("derived"); // 注册Derived类型,定义一个file scope的变量,通常应该置于Derived.cpp文件结尾
// 声明一个全局函数(简化操作,非必需)
lib::factory<Base>& glb_GetFactory( void)
{
return lib::factory<Base>::get_instance();
}
int main( int argc, char* argv[])
{
std::auto_ptr<Base> base=glb_GetFactory().create("base");
if ( base. get()){
base->print();
}
std::auto_ptr<Base> derived=glb_GetFactory().create("derived");
if (derived. get()){
derived->print();
}
system("pause");
return 0;
}
#include "AbstractFactory.h"
// 基类
class Base
{
public:
virtual void print( void){
std::cout<<"base class"<<std::endl;
}
};
lib::factory_register<Base, Base> reg1("base"); // 注册Base类型,定义一个file scope的变量,通常应该置于Base.cpp文件结尾
// 派生类
class Derived: public Base
{
public:
virtual void print( void){
std::cout<<"derived class"<<std::endl;
}
};
lib::factory_register<Base, Derived> reg2("derived"); // 注册Derived类型,定义一个file scope的变量,通常应该置于Derived.cpp文件结尾
// 声明一个全局函数(简化操作,非必需)
lib::factory<Base>& glb_GetFactory( void)
{
return lib::factory<Base>::get_instance();
}
int main( int argc, char* argv[])
{
std::auto_ptr<Base> base=glb_GetFactory().create("base");
if ( base. get()){
base->print();
}
std::auto_ptr<Base> derived=glb_GetFactory().create("derived");
if (derived. get()){
derived->print();
}
system("pause");
return 0;
}