Reputation: 1513
Currently working with a HUGE java class of an existing application. The initialization of this class reads in a lot of settings from the properties file to populate a large number of variables.
New functionality is being implemented which requires another dozen or so properties to be read in an additional dozen or so variables.
One option would be to just add the variables, and associated getter/setter calls.
Another option I thought about was to implement an Inner class and have the constructor accept the reference the properties file. Then it could use the reference to populate all the attributes. This inner class would house the associated getter/setter calls.
To help minimize clutter, what is the recommended way to work with such a class?
Upvotes: 2
Views: 48
Reputation: 6476
This is a highly opinionated question, but I would try to split the class into multiple classes. If you really need the one class, create several new classes in the same package and do the initialization in those, passing in the properties object from the constructor of the "main" class. In that, you would have all the getters for the configuration values, effectively creating a composition facade.
This could look like this:
public class Configuration {
private DBConfiguration dbConfiguration;
public Configuration() {
Properties props = readPropertiesFile();
dbConfiguration = new DBConfiguration(props);
}
public String getConnectionString() {
return dbConfiguration.getConnectionString();
}
}
[update] In addition to having cleaner code, it also allows you to gracefully @deprecate
the methods in the Configuration
class and slowly switch over to the more specialized configuration classes.
Upvotes: 1