我有一个Class(MyFactory),它生成A类型的对象。(myFactory将是一个实例) 我想允许用户扩展A类(例如,扩展到B类),并覆盖A的虚拟方法。
例如,不想扩展A:
的用户A* myObject = myFactoryInstance.createObject();
我目前用于扩展A与B的用户的技巧:
B* myObjectPrepare = new B()
myFactoryInstance.setNextObject( myObjectPrepare );
A* myObject = myFactoryInstance.createObject(); // Will look for a "next_object"
/* Here, myObject (which is myObjectPrepare modified by createObject)
methods will dynamically link to B class methods. */
我的目标是促进这项行动。
我试过了:
template <class T>
void MyFactory::set_requests_class() {
new T();
/* I don't want an instance, now. Just memorize class type
at compilation time, and make instances of T along exec
*/
}
(用法:set_requests_class();问题:我不想生成实例)
这可能是一个解决方案:
myFactoryInstance.createObject<YourObjectTypeHere>();
但我不想强制模板参数(属性的默认参数只能用于c ++ 11):我想让那些不想扩展A的用户非常简单。 由于同样的原因,我不能修改“createObject”签名。
(我未来的技巧将是实例化B对象,将其保留为属性并为新实例克隆它)
提前感谢(任何建议将不胜感激)
答案 0 :(得分:2)
最简单的方法就是重载createObject
。见这个例子:
class A
{
public:
A() { std::cout << "A"; }
};
class Subclass1 : public A
{
public:
Subclass1() { std::cout << "Subclass1"; }
};
class AFactory
{
public:
A* createInstance()
{
return new A();
}
template<typename Tsubclass>
A* createInstance()
{
return new Tsubclass();
}
};
int main()
{
AFactory fact;
A* pa = fact.createInstance();// creates A
A* psubclass1 = fact.createInstance<Subclass1>();// creates Subclass1
delete pa;
delete psubclass1;
return 0;
}
如果您需要保留您的设计,在实际创建对象之前设置要创建的类型,那么您可以使用如下的二级工厂来完成。虽然它不是一个非常好的设计;基本上工厂类现在基本上只是作为一个包装器来转换结果。看起来外面有更好的设计。
struct A
{
A() { std::cout << "A"; }
};
struct Subclass1 : A
{
Subclass1() { std::cout << "Subclass1"; }
};
struct GenericAFactoryBase
{
virtual A* createInstance() = 0;
};
template<typename Tsubclass>
struct GenericAFactory : GenericAFactoryBase
{
A* createInstance() { return new Tsubclass(); }
};
struct AFactory
{
std::shared_ptr<GenericAFactoryBase> genericFactory;
AFactory()
{
genericFactory.reset(new GenericAFactory<A>());
}
A* createInstance()
{
return genericFactory->createInstance();
}
template<typename Tsubclass>
void SetType()
{
genericFactory.reset(new GenericAFactory<Tsubclass>());
}
};
int main()
{
AFactory fact;
A* pa = fact.createInstance();// creates A
fact.SetType<Subclass1>();
A* psubclass1 = fact.createInstance();// creates Subclass1
delete pa;
delete psubclass1;
return 0;
}