I'm designing an API and I ended up having a few pure abstract classes. Because of the nature of the problem that I'm solving, each derived class has to be initialized with different sets of parameters. My solution was to basically define their constructors and pass the appropriate parameters when I'm making each class using the factory method. This makes the factory method rather complicated. Since I have to pass all possible parameters and then filter the appropriate parameters for each derived class and pass it around. It also opens up a path for error.
In order to avoid this complication, I would like to have the parameters as a type. So, I'm thinking to define struct BaseClassParams
and basically include everything that derived classes need into it and simply pass the param
to their constructor. This achieve what I want to do but since parameters of each derived class might differ from one to another, I want a better encapsulation. I thought doing something this:
class Base {
public:
struct BaseClassParams {
struct DerivedClass_A_Params {
};
struct DerivedClass_B_Params {
};
};
};
This way, I can encapsulate everything more neatly and in the factory method, only pass the appropriate parameter section to the derived class constructor. I wonder if you have faced this issue and if you have any suggestions? Overall what do you think of this approach and problem.
Edit: To clarify a bit, these derived classes are going to be used by a class, i.e., Manager. The Manager can choose to create different derived class, let's call them Employee, with different parameters. The Manager class can be initialized using a config file, i.e., JSON, or directly. If the Manager is created directly via the API, then Manager::Builder class can choose to create as many different employees with different parameters. So, I need to create these objects at run time.
P.S. I found this question and answer. The difference here is that my derived classes are not necessarily sharing parameters, they might have completely different parameters. This is kind of relevant too.
DerivedClassParams
, and now you need ... an abstract factory for the params you pass to your other abstract factory? This just splits the configuration part of a factory into two steps (parse+populate param object, then use param object) and I don't see any benefit in that. Or your param object is the union of all derived type params, and then it doesn't help at all with communicating which ones are actually used for a given derived type.