J
Joel Gilmore
Hi all,
I'm writing a java program (an evolution simulator) that has a large
number (>50) of preferences that can be tweaked (e.g., foodAvailable,
mutationRate, etc). They are booleans, ints and doubles. At present,
these are variables in a class, and I'm trying to work out the best
way for the user to be able to set these variables. Some of the most
important ones I've put into a preference box, but for the others that
will only get changed rarely, I'm trying to find a more sustainable
solution.
I envision having a user-editable config file which would be loaded at
the start, either in XML format or simply
foodAvailable = 20
mutationRate = 0.01
I know I could code a massive if-then-else block to handle all the
different variables (if (key="foodAvailable") foodAvailable=value,
but I would have to update that every time I add a new variable as the
program grows (prone to error, and just looks like bad code). Is there
a way that I could dynamically read in the variable name and attempt
to set it? It would also be very convenient to be able to do the
reverse and dynamically generate the config file from my class
(getting its variable names and their values).
I've heard about Reflection, but not certain if that's where I should
start. Another option would be to store the parameters not as
variables but in some sort of dictionary with String keys. However,
speed is an issue and I'm worried if I have to do a dictionary lookup
for every variable in my code it's going to slow things down
significantly (is that fear unfounded?). I could read them out of the
dictionary into the class variables, but that would again require hard
coding the variable names?
I don't know if enumerating the parameters instead of using them as
variables would make any difference?
Basically, I'm just not sure where to start looking in the Java help
files - so any suggested approaches would be much appreciated!
Cheers,
Joel
I'm writing a java program (an evolution simulator) that has a large
number (>50) of preferences that can be tweaked (e.g., foodAvailable,
mutationRate, etc). They are booleans, ints and doubles. At present,
these are variables in a class, and I'm trying to work out the best
way for the user to be able to set these variables. Some of the most
important ones I've put into a preference box, but for the others that
will only get changed rarely, I'm trying to find a more sustainable
solution.
I envision having a user-editable config file which would be loaded at
the start, either in XML format or simply
foodAvailable = 20
mutationRate = 0.01
I know I could code a massive if-then-else block to handle all the
different variables (if (key="foodAvailable") foodAvailable=value,
but I would have to update that every time I add a new variable as the
program grows (prone to error, and just looks like bad code). Is there
a way that I could dynamically read in the variable name and attempt
to set it? It would also be very convenient to be able to do the
reverse and dynamically generate the config file from my class
(getting its variable names and their values).
I've heard about Reflection, but not certain if that's where I should
start. Another option would be to store the parameters not as
variables but in some sort of dictionary with String keys. However,
speed is an issue and I'm worried if I have to do a dictionary lookup
for every variable in my code it's going to slow things down
significantly (is that fear unfounded?). I could read them out of the
dictionary into the class variables, but that would again require hard
coding the variable names?
I don't know if enumerating the parameters instead of using them as
variables would make any difference?
Basically, I'm just not sure where to start looking in the Java help
files - so any suggested approaches would be much appreciated!
Cheers,
Joel