Hi,
I have multiple implementations of a high-level objective. Basically, the objective is described in general language, for instance, "Choose one item out of N". Now, there can be any number of ways to implement this objective such as, random selection, prioritized selection, least recently used, etc. I have separate code/implementation for each of these different ways. The one thing they all have in common is the final objective, and some external interface functions. Now, I need to write a higher-level "interface" class that is used by another class which is agnostic (and really doesn't care) about which particular implementation is being used. This other class is only concerned with the high-level objective (and related common interface functions).
The specific way of the implementation of the high-level objective is specified by the user on the command line (so it is not known at compile time). Also, later on, additional ways of implementing the high-level objective may be written, and would just need to be "plugged-in", without requiring major modifications.
What is the best way to write this interface class?
As an example, the two ways of implementing the objective, A and B, are separate classes (written by different people). They may even have different constructor parameters:
class A {
// Parameters
public:
// Common interface
void choose_one(unsigned int& item);
};
class B {
// Parameters
public:
// Common interface
void choose_one(unsigned int& item);
};
Now the interface class C, should dynamically choose between implementation A and B, depending on user input
class C {
// All possible parameters (of A and B together)
// User's choice of A or B
public:
// Common interface
void choose_one(unsigned int& item);
};
I could naively instantiate both A and B, and use if() statements (to pick the chosen implementation) in class C's choose_one() function, but this will make code extremely slow. Implementations of A and B may be completely unrelated so there's no possibility of deriving one from the other, or overloading interfaces. A and B may be written by two developers who probably don't get along well ;). Also, another possible implementation, X, of the same objective may need to be added later on. So this interface class needs to be flexible enough to allow that. Of course, some modification of the interface class code can be done, but less is better.
I would appreciate if anyone can provide some ideas to do this. I hope my explanation of what I want to do is clear enough.
Thanks,
noob.