4

I am looking for concrete ideas of how to manage a lot of different parameter settings for my java program. I know this question is a bit diffuse but I need some ideas about the big picture so that my code becomes more maintainable.

What my project does is to perform many processing steps on data, mostly text. These processing steps are algorithms of varying complexity that often have many settings. I would also like to change which processing steps are used by e.g. configuration files.

The reason for my program is to do repeatable experiments, and because of this I need to be able to get a complete view of all the parameters used in the different parts of the code, preferably in a nice format.

At this (prototype) stage I have the settings in source code like:

public static final param1=0.35;

and each class that is responsible for some processing step has its own hard coded settings. It is actually quite scary because there is no simple way to change things or to even see what is done and with what parameters/settings.

My idea is to have a central key/value store for all settings that also supports a dump of all settings. Example:

k:"classA_parameter1",v:"0.35"
k:"classC_parameter5",v:"false"

However, I would not really like to just store the parameters as strings but have them associated to an actual java class or object.

Is it smarter to have a singleton "SettingsManager" that manages everything. Or to have a SettingsManager object in each class that main has access to? I don't really like storing string descriptions of the settings but I cant see any other way (Lets say one setting is a SAXparser implementation that is used and another parameter is a double, e.g. percentage) since I really don't want to store them as Objects and cast them.

Experience and links to pages about relevant design patterns is greatly appreciated.

To clarify, my experiments could be viewed as a series of algorithms that are working on data from files/databases. These algorithms are grouped into different classes depending on their task in the whole process, e.g.

Experiment //main
   InternetLookup //class that controls e.g. web scraping
      ThreadedWebScraper 
      LanguageDetection //from "text analysis" package
   Statistics    //Calculate and store statistics
      DatabaseAccess
   DecisionMaking //using the data that we have processed earlier, make decisions (machine learning)
      BuildModel
      Evaluate

Each of the lowest level classes have parameters and are different but I still want a to get a view of everything that is going on.

user1443778
  • 561
  • 5
  • 15

4 Answers4

8

You have the following options, starting with the simplest one:

  1. A Properties file
  2. Apache Commons Configuration
  3. Spring Framework

The latter allows creation of any Java object from an XML config file but note that it's a framework, not a library: this means that it affects the design of the whole application (it promotes the Inversion of Control pattern).

Pino
  • 6,292
  • 5
  • 42
  • 54
3

This wheel has been invented multiple times already.

From the most basic java.util.Properties to the more advanced frameworks like Spring, which offers advanced features like value injection and type conversion.

Building it yourself is probably the worst approach.

pap
  • 25,288
  • 6
  • 37
  • 46
3

Maybe not a complete answer to your question, but some points to consider:

Storing values as strings (and parsing the strings into other types via your SettingsManager) is the usual approach. If your configuration value is too complex to do this then it's probably not really a configuration value, but part of your implementation.

Consider injecting the individual configuration values required by each class via constructor arguments, rather than just passing in the whole SettingsManager object (see Law of Demeter)

Avoid creating a Singleton SettingsManager if possible, singletons harm testability and damage the design of your application in various ways.

Community
  • 1
  • 1
codebox
  • 18,210
  • 7
  • 54
  • 77
1

If the number of parameters is big I would split them to several config files. Apache Commons Configuration, as mentioned by @Pino is really a nice library to handle them. On the Java-side I would probably create one config-class per file and wrap Commons Configuration config to load settings, eg:

class StatisticsConfig {

   private Configuration config = ... ;

   public double getParameter1() {
        return config.getDouble("classA_parameter1");
   }
}

This may need quite a lot of boilerplate code if the number of parameters is big but I think it is quite clean solution (and easy to refactor).

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Adam Dyga
  • 8,056
  • 3
  • 24
  • 34