I have a design problem.
I have a polymorphic structure with an interface A (abstract one) and a workflow implemented in class W which uses the interface A without knowing the derived classes. This is implemented in DLL1 and I have a factory interface F capable of returning A*.
In DLL2, I have the concrete implementations of A, which could be A1, A2 etc. and a factory implementation F1 capable of creating A1 and A2 instances.
The factory interface which I have is something like this.
enum ObjectType{typeA1, typeA2};
class F
{
public:
A* create(enum ObjectType) = 0;
}
Some client class which knows both DLL1 and DLL2 will give me the concrete object type.
But this is ugly since I will have to know the possible types of the concrete classes upfront when I write my DLL1. And this almost defeats my purpose of the polymorphic design. I don’t want to do this.
The alternative option which I can think of is to use strings, instead of the enumeration. But, I like to avoid strings as they are not type-safe and are prone to errors. I wish I had a chance to extend the enums by inheritance, just like the classes.
My questions :
Is there a better way out?
Is extension of enums possible in C++11? (I don’t have it now, though)
You can modify your abstraction to allow the user of interface
Ato also provide the particular factory that creates it. (instead of providing anenum).Now, each
ConcreteF<>would need to be passed into code to DLL1 for those parts of the framework that need to create the right concrete instance ofA. Thus, the framework user must inform the framework which ofA1orA2to create not by passing in anObjectType, but by passing in the correctConcreteF(namelyConcreteF<A1>orConcreteF<A2>).