Design pattern for parameter parameters supported in a java project for decent size

I am looking for specific ideas on how to control many different parameters for my java program. I know this question is a bit blurry, but I need some ideas about the big picture to make my code more convenient.

What my project does is the implementation of many stages of data processing, mainly text. These processing steps are algorithms of varying complexity, which often have many settings. I would also like to change which processing steps are used, for example, configuration files.

The reason for my program is to perform repeated experiments, and because of this I need to be able to get a complete picture of all the parameters used in different parts of the code, preferably in a good format.

At this stage (prototype), I have settings in the source code, for example:

public static final param1=0.35; 

and each class responsible for a certain processing step has its own hard-coded settings. This is actually quite scary, because there is no easy way to change the situation or even see what has been done and with what parameters / settings.

My idea is to have a central repository of keys / values ​​for all settings, which also supports resetting all settings. Example:

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

However, I would not want to just store the parameters as strings, but associate them with the actual class or java object.

Is it smarter to have a singleton "SettingsManager" that controls everything. Or to have a SettingsManager object in each class that the master has access to? I don’t really like to store string descriptions of settings, but I don’t see another way (say, one parameter is the SAXparser implementation that is used, and the other parameter is double, for example, percentage), since I really do not want to store them as Objects and throw them.

Experience and links to pages on relevant design patterns are welcome.

To clarify, my experiments could be considered as a series of algorithms that work with data from files / databases. These algorithms are grouped into different classes depending on their task in the whole process, for example.

 Experiment //main InternetLookup //class that controls eg 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 lower-level classes has parameters and is different, but I still want to get an idea of ​​everything that happens.

+4
source share
4 answers

You have the following options, starting with the simplest:

The latter allows you to create any Java object from an XML configuration file, but note that it is a framework and not a library: this means that it affects the design of the entire application (it supports a control inversion template).

+8
source

This wheel has already been invented several times.

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

Building yourself is probably the worst approach.

+3
source

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

Saving values ​​as strings (and parsing strings into other types using the settings manager) is a common approach. If your configuration value is too complicated for this, this is probably not really a configuration value, but part of your implementation.

Consider entering the individual configuration values ​​needed for each class through the constructor arguments, and not just to pass the entire SettingsManager object (see Demeter Law )

Avoid creating the Singleton SettingsManager, if possible, single-player games harm testability and damage the design of your application in various ways.

+3
source

If the number of parameters is large, I would split them into several configuration files. Apache Commons configuration, as @Pino mentioned, is a really good library for handling them. On the Java side, I would probably create one configuration class for each file and exchange the Commons Configuration configuration to load the settings, for example:

 class StatisticsConfig { private Configuration config = ... ; public double getParameter1() { return config.getDouble("classA_parameter1"); } } 

It might take quite a bit of template code if the number of parameters is large, but I think this is a pretty clean solution (and easy to refactor).

+1
source

Source: https://habr.com/ru/post/1436531/


All Articles