How does one provide a unified interface to sets of functions, that are used in the same way? To illustrate, please look at the set of given library functions:
/* existing library functions */
/* the signatures are different: some return int, some float */
/* set of input related functions */
int getInputValue() { return 42; }
size_t getInputSize() { return 1; }
/* set of output related functions */
int getOutputValue() { return 21; }
size_t getOutputSize() { return 1; }
/* set of parameter related functions */
float getParameterValue() { return 3.14; }
size_t getParameterSize() { return 1; }
and assume they are used in the same way:
if (getSize() > 0) {
T value = getValue()
A) What is a good way to provide getSize()
and getValue()
?
I first though that Template Method Pattern is what I want, but I couldn't apply it, because in contrast to the Worker in the Template Method Pattern, my functions have different signatures.
So what I did instead:
/* I want to provide a uniform interface */
/* the specific part of inputs, outputs and parameters is in the traits */
struct input_traits {
typedef int value_type;
static int getValue() { return getInputValue(); }
static size_t getSize() { return getInputSize(); }
};
struct output_traits {
typedef int value_type;
static int getValue() { return getOutputValue(); }
static size_t getSize() { return getOutputSize(); }
};
struct parameter_traits {
typedef float value_type;
static float getValue() { return getParameterValue(); }
static size_t getSize() { return getParameterSize(); }
};
/* the common part (they are used in the same way) is in the Helper */
template<typename traits>
class CommonUsage {
public:
void use()
{
if (traits::getSize() > 0) {
typename traits::value_type value = traits::getValue();
}
}
};
int main()
{
CommonUsage<input_traits>().use();
CommonUsage<output_traits>().use();
CommonUsage<parameter_traits>().use();
}
B) Is this a good approach?
A. If i understood your question correctly you should use an abstract class. Look at the next code, it essentially does the same as your code.
This is how i would do it.