C++11 模板类层次结构的对象工厂注册 我遵循Andrei Alexandrescu的《现代C++设计指南》实现了一个通用对象工厂,这样我就可以定义一个类层次结构(我将以最简单的方式编写代码,避免实现细节或内存分配/释放问题;我知道这些事情,我想集中讨论主要问题):

C++11 模板类层次结构的对象工厂注册 我遵循Andrei Alexandrescu的《现代C++设计指南》实现了一个通用对象工厂,这样我就可以定义一个类层次结构(我将以最简单的方式编写代码,避免实现细节或内存分配/释放问题;我知道这些事情,我想集中讨论主要问题):,c++11,templates,design-patterns,factory,C++11,Templates,Design Patterns,Factory,因此,想要在代码中使用此层次结构的用户只需使用适当的标识符调用Base::Factory方法: // File main.cpp #include"base.h" int main(){ Base* pb = Base::Factory("Derived_1"); // Do stuff with pb return 0; } 现在,假设我有一个模板类层次结构,比如: // File "baset.h" #include <string> #includ

因此,想要在代码中使用此层次结构的用户只需使用适当的标识符调用
Base::Factory
方法:

// File main.cpp
#include"base.h"
int main(){
    Base* pb = Base::Factory("Derived_1");

    // Do stuff with pb

    return 0;
}
现在,假设我有一个模板类层次结构,比如:

// File "baset.h"
#include <string>
#include "singleton.h"
#include "object_factory.h"

template<class T>
class BaseT;

template<class T>
using SingletonBaseTFactory = Singleton
<
    ObjectFactory
    <
        BaseT<T>,         // Abstract product type
        std::string,      // Identifier type
        BaseT<T>* (*)()   // Concrete product creator type
    >
>;

template<class T>
class BaseT {
    /*Define the interface*/ 
public:
    BaseT Factory(const std::string& ID) {
        return SingletonBaseTFactory<T>::Instance().Factory(ID);
    }
};

// File "derivedt_1.h"
#include "baset.h"
template<class T>
class DerivedT_1 : public BaseT<T> { /* ... */ };
//文件“baset.h”
#包括
#包括“singleton.h”
#包括“object_factory.h”
模板
基类;
模板
使用SingletonBaseTFactory=Singleton
<
对象工厂
<
BaseT,//抽象产品类型
std::string,//标识符类型
BaseT*(*)()//具体的产品创建者类型
>
>;
模板
基类{
/*定义接口*/
公众:
BaseT工厂(常量标准::字符串和ID){
返回SingletonBaseTFactory::Instance().Factory(ID);
}
};
//文件“derivedt_1.h”
#包括“baset.h”
模板
类DerivedT_1:public BaseT{/*…*/};
在这种情况下,在使用类层次结构之前,注册是用户对其想要使用的每种类型的责任:

// File main.cpp
#include "baset.h"
#include "derivedt_1.h"

bool register_derived_1_int = SingletonBaseTFactory<int>::Instance().Register("Derived_1", [](){ return new DerivedT_1<int>; });

int main() {
    BaseT<int>* pb = BaseT<int>::Factory("Derived_1");
    return 0;
}
//文件main.cpp
#包括“baset.h”
#包括“derivedt_1.h”
bool register_-derived_1_int=SingletonBaseTFactory::Instance().register(“derived_1”,[](){return new-DerivedT_1;});
int main(){
BaseT*pb=BaseT::工厂(“派生_1”);
返回0;
}
请记住,每个派生模板类的ID对于每个类型都是相同的
T
,即使在模板化的情况下,将注册责任委托给每个派生类的开发人员(而不是用户)是否有意义? 如果是,是否有一个解决方法来实现它

编辑:

我发现这项工作解决了模板化案例中的对象工厂注册问题。 但是,派生类的注册仍然是知道派生类可以实例化的类型的人的责任。 通常,类开发人员没有这样的知识-用户有。 那么,还有什么方法可以免除用户注册派生类的责任呢

// File "baset.h"
#include <string>
#include "singleton.h"
#include "object_factory.h"

template<class T>
class BaseT;

template<class T>
using SingletonBaseTFactory = Singleton
<
    ObjectFactory
    <
        BaseT<T>,         // Abstract product type
        std::string,      // Identifier type
        BaseT<T>* (*)()   // Concrete product creator type
    >
>;

template<class T>
class BaseT {
    /*Define the interface*/ 
public:
    BaseT Factory(const std::string& ID) {
        return SingletonBaseTFactory<T>::Instance().Factory(ID);
    }
};

// File "derivedt_1.h"
#include "baset.h"
template<class T>
class DerivedT_1 : public BaseT<T> { /* ... */ };
// File main.cpp
#include "baset.h"
#include "derivedt_1.h"

bool register_derived_1_int = SingletonBaseTFactory<int>::Instance().Register("Derived_1", [](){ return new DerivedT_1<int>; });

int main() {
    BaseT<int>* pb = BaseT<int>::Factory("Derived_1");
    return 0;
}