c++design-patternsfactorystd-function

Is there any reason to use virtual classes for factory method over map of functions in C++?


I'm implementing factory method for my polymorphic class. The "traditional" way to do so would be to have two families of classes: for objects themselves and for their factories. In this case my classes would look like this:

struct Base {
  virtual void foo(/*...*/) = 0;
  virtual ~Base() = default;
};

struct A: Base {
  void foo(/*...*/) override;
};

struct BaseFactory {
  virtual std::unique_ptr<Base> create(/*...*/) = 0;
};

struct AFactory : BaseFactory {
  std::unique_ptr<Base> create(/*...*/) override; // returns pointer to A
};

std::map<std::string, std::unique_ptr<BaseFactory>> factories;
// this map gets filled somewhere, not the point of question

In this case I could create an object of type A using factories["A"]->create(/*...*/);. If I wanted to add a new implementation of Base, I would have to write a new factory. However, I could avoid using virtual functions for factory alltogether using map of functions:

std::map<std::string, std::function<std::unique_ptr<Base>(/*...*/)> factories;

In this case I would create an object with factories["A"](/*...*/);. I'm wondering, what may be the drawbacks of the second way? In such case my factories can't have any other member functions, but I think it's fine, as long as sole purpose of my factory is to create objects.


Solution

  • A factory is useful when:

    However, if your use case is simple, then sure - an std::function (perhaps capturing some extra parameters necessary for creation) or a plain function pointer (if capturing nothing) suffice. Either way is good, it all depends on complexity and verbosity.

    The more complex case, the more verbose solution is better to make it clearer what is happening. If you hide/capture too much, a future reader (e.g. future yourself) may have problems understanding what is happening.