views:

272

answers:

4

Say I have ini/json to store configuration setting of my desktop application,Will it be ok to have a static object with all property loaded at startup/when ever it is required or is there any other better alternative?

Since this is the very first time I am doing this ,so just wanted to know whether static object is fine or singleton pattern or something else would be better

+2  A: 

Either way is fine.

If it was me, I would have it on construction of the config object.

cConfig Config("config.ini");

This Config class would load the settings found in the file. Any code can access the settings by doing

Config.Get("NumberOfFoobars")

For testability purposes, if there is no file in the construction, the class' settings is set to default or a log file is created with a line advising the user of the missing settings.

And then for functions that needs the config, I would pass the Config instance as part of the parameters:

DoStuff(Config, [...]);

and have DoStuff get the variables from the Config class.

This makes the class testable (you can mock Config class), readeable (at a glance, you can tell which function requires Configs) and you don't have to rely on static instances (singletons are dangerous if you don't know how to use them).

You might be interested to learn more about this

MrValdez
where cConfig it is allocated ,scope?
yesraaj
Depends. If it was me, I would allocate it at main or at your app's entry point. You could make it global but it might turn into a singleton, so I don't recommend this.
MrValdez
By the way, this is called Dependency Injection http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/
MrValdez
A: 

If the config settings is going to be modified by the user to determine the way the application is going to run, then it might be better to keep it in some ini file. Some users like to edit ini files directly rather than do it through the GUI. It is better to give both the options.
Also some user have multiple ini files and rotate among them for settings they need at that point of time.

Manoj
this is how i am thinking too
yesraaj
A: 

Loading all your settings at startup (or the first time a setting is needed) will work most of the time, but the user will have to restart the application in order for any edits to the config file to take effect. For most users this will never be an issue, but for advanced users who like to edit config files directly this could be frustrating. On UNIX based OSs it is possible to handle the SIGHUP signal which has become a accepted trigger to re-read configuration files. There is no similar method that I know of for Windows. An alternative approach would be to keep track of the modification time of the config file to determine if the settings should be re-read.

Mark Goddard
planning to have a button to reload setting
yesraaj
That makes sense for an advanced program where the user is familiar with the concept of configuration files. It might confuse a basic user.
Mark Goddard
+1  A: 

I usually use Boost.Program_options, and I usually use a singleton.

rlbond