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.

You mean something on the order of this:

#include <iostream>
#include <string>
using namespace std;

class A
{
public:
    A() {a = 0;}
    void common_interface()
    {
        cout << "class A\n";
    }
protected:
    int a;
};

class B
{
public:
    B() {b = 0;}
    void common_interface()
    {
        cout << "class B\n";
    }
protected:
    int b;
};

class C : public A, public B
{
public:
    C() {};
    void common_interface()
    {
        a = b = 1;
        cout << "Hello World\n" << a << " " << b << "\n";
    }
};

int main()
{
    C c;
    c.A::common_interface();
    c.B::common_interface();
    c.common_interface();
}

Well, not exactly. Class C is more of a wrapper class that is supposed to dynamically choose between implementations A and B, depending on user input. This user input once given at the beginning of the program (command-line), would stay fixed throughout. So, once configured, either A or B will be used. Class C interacts with an outside class (say, D) that knows nothing of A or B.

Following your suggestion, I would still need to do something like:

class C::common_interface(void) {
if (user_choice == A)
  c.A::common_interface();
else // user_choice = B
  c.B::common_interface();
}

I wanted to avoid this if/else because C::common_interface() will be called several millions of times, and the branch test (if/else) may incur a penalty.

I was wondering if this could be avoided using clever pointer tricks or callbacks or something smart like that, so that once the user choice is specified and respective constructors called, the program can call C::common_interface() and run faster without have to check the user choice every time.

I hope there some way to do this. Ancient Dragon, I appreciate your help.

You could do it with function pointers or even entire DLLs. If the program is large then I'd put the interfaces into different dlls so that it can be loaded then the main application program starts up, by calling win32 api function LoadLibrary() -- assuming this is a MS-Windows program.

This article has been dead for over six months. Start a new discussion instead.