Hi,
I know OOP is a black sheep here, but I would like to hear your views on some of the simpler forms of OOP. And maybe this could even be solved by non OOP methods.
How to group similar code implementations to be able to reuse and/or select one for testing. For instance different methods of random number generation. What if you wanted to replace the pseudo implementation we have now with a new one, and wanted to compare them.
From the usage code we have now concerning random series we already have somewhat of an API. And although some might argue agains it, this API could be represented as the following interface (in no perticular language pseudo code):
| interface Random {
void Seed(int);
uint32 RandomNextUInt32();
uint32 RandomChoice(uint32 ChoiceCount);
real32 RandomUnilateral();
real32 RandomBilateral();
real32 RandomBetween(real32 Min, real32 Max);
int32 RandomBetween(int32 Min, int32 Max);
}
|
The current implementation with the table could then be used like:
| RandomTable series;
series.Seed(1234);
x = series.RandomChoice(2);
|
This is not really any different from the straight C code.
Changing to another implementation could then be a single line change:
The implications of using this setup with normal OOP practices with virtual functions already brings one downside: the vtable. But this could probably be implemented with just straight classes, without virtual functions.
If both RandomTable and PRNGRandom implement the same signatures, they could be used in the same places. The only problem I see is passing them to other functions. Storing the series in GameState could be done trivially and that would preserve the type information and thus the implementation used (function pointers will be hardcoded by compiler).
But passing them in functions without changing the parameter type would require some sort of BaseClass to be able to keep the parameters of the function the same. This will probably require templating like
CRTP or the vtable which would like to prevent.
What are your thoughts on this? Is there a way to do this in straight C? Or would this really require search/replace of the implementation calls?
PS. I'm also open to the discussion of whether I should be thinking about this at all. Since I'm in the process of trying to go back to my coding roots by going away from OOP. But it is hard to completely give up, when I keep seeing re-usable parts of code like Random number generators, that already have a bit of OOP style in them with the "this" pointer as first parameter.