I am faced with the following conundrum: Our software has an abstract base class for algorithm objects. All these objects have a common execute() method, e.g.:
class Algorithm
{
public:
// [...]
virtual void execute() = 0;
// [...]
};
For each algorithm that we want to implement, we simply inherit from the base class and store all loaded algorithm objects in a central location. So far, so good.
The problem now lies in the parameters of an algorithm. We want to be able to describe for each algorithm the parameters that need to be set (by an external class). To this end, we gave each algorithm a ParameterList object that contains its parameters. I should clarify that for us, a parameter consists of some kind of type (such as int) and a label (such as “Number of iterations).
The problem now begins when we want to connect the ParameterList to some kind of GUI. Obviously, our algorithms should have no “knowledge” of the graphical API (Qt, GTK, etc.) that we are using. Yet, on the same side, we want to be able to describe algorithm paramters semantically, for example by specifying that the algorithm requires a filename. How this filename is then displayed is up to the GUI.
Is there a way to combine this ParameterList with some kind of semantic type knowledge?
I realize that this question sounds very vague. However, I am not permitted to post any non-trivial code examples (for NDA reasons). So, has anyone been faced with a similar problem in the past?
To wrap it up: We want our objects to describe the parameters they require to a GUI, without knowing the exact details of the GUI.
One option here might be to use the visitor pattern. You can create a base class like this:
You could define subclasses like these:
Note that in each
acceptmember function, the type of*thisis the static type of the class – namely,IntParameter&in the first case, andFilenameParameter&in the second case.You can then define a base class
ParameterVisitorclass like this:You can then subclass this visitor to get back the type information:
Your
ParameterListclass can then just store a list ofParameter*s. You can then build the GUI by instantiating the appropriate visitor type and then having itsvisitcallbacks do all the widget construction. This ends up being type-safe and recovers the information you need. It does have the drawback that every time you create a new parameter type, you have to add a new member functionvisitto theParameterVisitorclass, but you’d need to do that anyway to do all the GUI building.Hope this helps!