Parameters / configurations / settings view
Okay, 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 options / configuration / options / settings / etc. These can be the initial values of certain variables, or simply values that are not associated with a change, except for sessions. How should I represent these values in my data model?
My initial thought is to just keep all the values in one class that are globally available (maybe one singleton) as these values will be used in different places in the code. This, however, will make all other classes dependent on this singleton.
Is this a problem in this situation? Is there another perfect solution that I haven't thought of? How do you usually represent this type of data? Is there anything else I should know about this issue?
thanks
source to share
You might want to use dependency injection. Create an interface IConfigurationProvider
. Create a class DefaultConfigurationProvider
that implements this interface and does the actual task. Makes other objects that rely on configuration data, waits for an instance, IConfigurationProvider
and provides them with an instance DefaultConfigurationProvider
. Most dependency injection frameworks will handle this for you.
source to share
"Is this a problem [with the Singleton configuration]?
Not really.
"all other classes that depend on this singleton"
Not really. You can easily create things so that some classes get configuration and provide it to other classes.
It is a matter of resposnsibility - which class / package / method is responsible for this configuration issue. And the answer should be very, very few classes need to know the configuration.
For example, your model should probably focus on an implementation-independent problem area. It may not need any configuration.
"Is there another perfect solution that I haven't thought of?"
Not really. The configuration problem is complex. You can have default configuration, override default configuration, command line options. It is quite difficult.
"How do you usually represent this type of data?"
A Singleton for the most part. Typically, several key "top-level" classes in a given application need to know the configuration and use this information to customize all other classes.
For the command line, the class that is called by the "main" program to parse arguments will also know about the configuration. What is it.
For a web application, the top level web application part uses configuration to create, configure, customize, or create other things. In many cases, the Factory Key will use the configuration to decide what to build. A Key Builder may require customization to customize the composite objects in its building.
The configuration is not global. This is not visible to all classes. It is - like a database - a valuable resource, and it is carefully wrapped in classes that are responsible for handling configuration.
"Is there anything else I should know about this issue?"
Yes. The Singleton configuration is not global. It is a rare, valuable resource that is used in several key locations. Less is better.
source to share
It depends on your specific situation.
For example, you can provide a Context class with a getParameter (string name) and setParameter (string name, object value) method and many other methods needed to store configuration parameters.
You can pass the Context as a parameter in the constructor of the class that config should use. Parameters:
public class ConfigUser {
private Context c;
public ConfigUser (Context c) {
this.c = c;
}
...
}
source to share