Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for parameter settings that is maintainable in decent size java project

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.

like image 720
user1443778 Avatar asked Dec 06 '22 12:12

user1443778


2 Answers

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).

like image 190
Pino Avatar answered May 01 '23 12:05

Pino


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.

like image 34
pap Avatar answered May 01 '23 12:05

pap