tags:

views:

58

answers:

3

Alright, so let's say I'm writing an application using an object-oriented language, and I have a set of key-value pairs that represent program parameters/configuration/options/settings/etc. These may be initial values of certain variables, or just values that aren't bound to change except between sessions. How should I represent these values in my data model?

My initial thought is to simply store all the values in single class that is globally accessible (singleton perhaps), because these values will be used in various places in the code. This, however, will make all the other classes dependent on this singleton.

Is that a problem in this situation? Is there another, ideal solution that I haven't thought of? How do you people usually represent this type of data? Is there anything else I should be aware of concerning this problem?

Thanks

+1  A: 

You might want to use dependency injection. Create an interface IConfigurationProvider. Create a class DefaultConfigurationProvider that implements that interface and does the actual task. Makes other object that rely on config data expect an IConfigurationProvider instance and provide them with an instance of DefaultConfigurationProvider. Most dependency injection frameworks will handle this for you.

Mehrdad Afshari
After studying and discussing this method with friends, it seems to be a very neat way of solving my problem. I will try to use this technique and report back. Vote up for the suggestion!
Lucas Lindström
A: 

It depends on your concrete situation.

For example, you can provide class Context with method getParameter (String name) and setParameter (String name, Object value) and many other methods which you need to preserve config parameters.

You can transfer Context as a parameter in constructor of class which should use config. parameters:

public class ConfigUser {
   private Context c;
   public ConfigUser (Context c) {
      this.c = c;
   }
   ...
}
Roman
+1  A: 

"Is that a problem [with a Singleton configuration]?

Not really.

"all the other classes dependent on this singleton"

Not really. You can easily design things so that some classes get configuration, and provide it to other classes.

This is a question of resposnsibility -- what class/package/method has responsibility for this configuration issue. And the answer should be very, very few classes need to know the configuration.

Your model, for example, should probably be focused on the problem domain independent of implementation nuance. It may not need any configuration.

"Is there another, ideal solution that I haven't thought of?"

Not really. The configuration problem is hard. You may have a default configuration, overrides to the default configuration, command-line options. It's quite complex.

"How do you people usually represent this type of data?"

A Singleton for the most part. Generally, a few key "top-level" classes in a given application must know the configuration and use that information to configure all of the other classes.

For a command-line program, the class that is invoked by the "main" program to parse arguments will also know about the configuration. And that's it.

For a web app, a top-level piece of the web app uses the configuration to build, configure, customize or instantiate other things. In many cases, a key Factory will use the configuration to decide what to create. A key Builder may require configuration to customize composite objects its building.

Configuration is not "global". It is not seen by all classes. It is -- like the database -- a precious resource and it is carefully wrapped by classes that have responsibility for handling configuration.

"Is there anything else I should be aware of concerning this problem?"

Yes. The configuration Singleton is not global. It is a scarce, precious resource that is used in a few key places. The fewer the better.

S.Lott