R
Raymond Martineau
In C, it's possible to create an array of the following data structure to
associate a function call with a specific key or identifier:
struct {
char *key;
void (*function)();
} a[] = { { "Function1", Function1} ,
{ "Function2", Function2}};
(Or some other varient that you are comfortable with.)
I would like to know the appropriate procedure on how to implement the same
in Java. I would prefer to use the above system, as it can be much quicker
to call a function by looking up the associated token in a hashtable as
opposed to writing many lines of an if/else ladder.
The first thought that appeared in my mind would involve reflecting
classes. Checking with the API spec indicated that it should work, but I'm
not sure that placing a large number of methods in one class is the best
solution.
In case you're wondering, I'm using this system to interpret a language
that uses a large number of keywords (with each keyword executing its own
function.)
associate a function call with a specific key or identifier:
struct {
char *key;
void (*function)();
} a[] = { { "Function1", Function1} ,
{ "Function2", Function2}};
(Or some other varient that you are comfortable with.)
I would like to know the appropriate procedure on how to implement the same
in Java. I would prefer to use the above system, as it can be much quicker
to call a function by looking up the associated token in a hashtable as
opposed to writing many lines of an if/else ladder.
The first thought that appeared in my mind would involve reflecting
classes. Checking with the API spec indicated that it should work, but I'm
not sure that placing a large number of methods in one class is the best
solution.
In case you're wondering, I'm using this system to interpret a language
that uses a large number of keywords (with each keyword executing its own
function.)