在网上搜索了工厂模式的不同方法后,我实现了自己的版本,我对此非常满意。Register成员函数为模板类型创建一个函数指针,使用索引作为键将其存储在std::map中。下面的代码可以轻松编译和运行(Windows 7 64 位,带 GCC 的 Code::Blocks 10.05)。
#ifndef OBJECT_FACTORY_HPP
#define OBJECT_FACTORY_HPP
#include <map>
namespace internal {
template<typename BaseType, typename ObjectType>
BaseType* CreateFunction() {
return new ObjectType;
}
}
template<typename BaseType, typename IndexType>
class ObjectFactory {
public:
ObjectFactory();
template<typename ObjectType>
bool Register(const IndexType& index);
bool Unregister(const IndexType& index);
BaseType* Create(const IndexType& index);
private:
typedef BaseType* (*creationCallback)();
typedef std::map<IndexType, creationCallback> Registry;
Registry registry;
// private:
// template<typename ObjectType>
// BaseType* CreateFunction();
};
template<typename BaseType, typename IndexType>
ObjectFactory<BaseType, IndexType>::ObjectFactory() {
registry.clear();
}
template<typename BaseType, typename IndexType>
template<typename ObjectType>
bool ObjectFactory<BaseType, IndexType>::Register(const IndexType& index) {
if (registry.find(index) != registry.end())
return false;
registry[index] = &internal::CreateFunction<BaseType, ObjectType>;
// registry[index] = &CreateFunction<ObjectType>; <-- FAILS!
return true;
}
template<typename BaseType, typename IndexType>
bool ObjectFactory<BaseType, IndexType>::Unregister(const IndexType& type) {
if (registry.find(type) == registry.end())
return false;
return (registry.erase(type) == 1);
}
template<typename BaseType, typename IndexType>
BaseType* ObjectFactory<BaseType, IndexType>::Create(const IndexType& index) {
if (registry.find(index) == registry.end())
return NULL;
return registry[index]();
}
//template<typename BaseType, typename IndexType>
//template<typename ObjectType>
//BaseType* ObjectFactory<BaseType, IndexType>::CreateFunction() {
// return new ObjectType();
//}
#endif
我最初的方法是让CreateFunction作为私有成员以对用户隐藏它(查看注释部分,注意非成员函数需要一个额外的模板参数)。但是,这会失败并显示以下错误消息,所有错误消息都指向我在Register成员函数中存储函数指针的行:
In member function 'bool ObjectFactory<BaseType, IndexType>::Register(const IndexType&) [with ObjectType = Triangle, BaseType = Shape, IndexType = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]':|
instantiated from here
error: no matches converting function 'CreateFunction' to type 'class Shape* (*)()'|
error: candidates are: template<class ObjectType> BaseType* ObjectFactory::CreateFunction() [with ObjectType = ObjectType, BaseType = Shape, IndexType = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]|
我写了一个小测试客户端进行测试:
#include "ObjectFactory.hpp"
#include <iostream>
class Shape {
public:
Shape() {}
virtual void print() { std::cout << "At heart, I'm a shape"; }
};
class Triangle : public Shape {
public:
Triangle() {}
void print() { Shape::print(); std::cout << ", but I'm truly a triangle" << std::endl; }
};
int main(int argc, char* argv[]) {
ObjectFactory<Shape, std::string> objectFactory;
objectFactory.Register<Triangle>("triangle");
Shape* triangle = objectFactory.Create("triangle");
triangle->print();
delete triangle;
return 0;
}
这甚至可能吗?我觉得应该是,而且我知道我以某种方式调用了不正确的成员函数,但我不明白为什么。在相关说明中,由于有人可能会提到它,我计划使用 *boost::shared_ptr* 进行分配,而不是普通的 new 运算符;)也欢迎任何其他关于实施的建议或建议。