views:

123

answers:

2

Hi, i'm developing a simple simulation with OpenGL and this simulation has some global constants that are changed by the user during the simulation execution. I would like to know if the Singleton design pattern is the best way to work as a temporary, execution time, "configuration repository"

+1  A: 

I think in the past I've used namespaces for this purpose, not singleton classes, but this should work too (probably even better).

Of course, if you want to be able to change the configuration without recompiling, you might want to move everything to a separate properties file (or XML or YAML or CSV or whatever you prefer) and then load it at application start up. Then you WOULD need a "config" class to store all the values in a hashmap (or something like that).

FrustratedWithFormsDesigner
+3  A: 

A singleton is probably the best option if you need to keep these settings truly "global".

However, for simulation purposes, I'd consider whether you can design your algorithms to pass a reference to a configuration instance, instead. This would make it much easier to store configurations per simulation, and eventually allow you to process multiple simulations with separate configurations concurrently, if requirements change.

Often, trying to avoid global state is a better, long term approach.

Reed Copsey