I'm attempting to design a factory class that allows me to encapsulate the construction of any class (that has been derived from the same base class "Base"), with any number of constructor arguments. The current design only supports one or zero constructor arguments:
class FactoryBase
{
public:
FactoryBase(std::string id) : id_(id) {}
virtual ~FactoryBase(){}
virtual Base* create() const = 0;
std::string getId() const { return id_; }
protected:
std::string id_;
};
template<class T, typename A> //One argument
class Factory : public FactoryBase
{
public:
Factory(std::string id, A arg) : FactoryBase(id), arg_(arg) {}
Base* create() const { return new T(arg_); }
private:
A arg_;
};
template<class T> //zero arguments/default ctor
class Factory<T,void> : public FactoryBase
{
public:
Factory(std::string id) : FactoryBase(id) {}
Base* create() const { return new T(); }
};
I could just add one template specialization for each number of arguments, but I'd like to learn how to do it "porperly".