2

Say I have some program-wide information, like the root directory of the program or the language selected, that I would like the user to be able to change. How should I best go about storing this information in a form easily accessible at any time for any part of the program?

I was thinking of using global variables stored inside an individual file that could be included by other files that need the information. This way, I would just need to change the value of that global variable and the whole program could access the new value as it did before. However, I am afraid that using such global variables would be bad practice. What are your thoughts on this?

(Also, I thought that this would be language-agnostic, but if there are different best ways of accomplishing this in different languages, I would like to hear about that as well)

wrongusername
  • 18,564
  • 40
  • 130
  • 214

2 Answers2

3

Sounds like a preferences API. Virtually every nontrivial application framework offers one these days, or even several. There's one as part of the core Java API, for example, and another as part of the Eclipse framework. Windows has the registry, which you can use from any language on that platform, etc. These things are normally structured so there's a static factory method you use to get ahold of the preference store for the application, and then you can interact with it from anywhere.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • +1 Android, Mac OS X, and the KDE and Gnome environments on Linux also provide preferences APIs. – Theran Aug 17 '11 at 03:49
0

You may create a thing called shared controller for complex information storage (with some methods to create/update/delete the information). It can be implemented following the singleton pattern. Objective-C developers often use that practice to store global objects across application.

Daniel O'Hara
  • 13,307
  • 3
  • 46
  • 68