Dozens of getter methods vs a single get method [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this questionI got into a discussion with a colleague regarding the following issue:
Our project reads in a config file and stores dozens of parameters. He's stored this data in named variables together with dozens of getter methods for each variable. In my opinion, this has made the class overly long with getter methods and difficult to maintain.
private var;
public String getVar() {
return var;
}
// This appears dozens of times in the class
......
My solution is to store key values pairs in a map, and have a single getValue(String key) method which takes as argument a key which represents each variable. The keys will be stored as a list of constants in a Config class which will also handle reading the data from file.
Config c = new Config();
c.readConfig(someFile);
...
开发者_如何转开发c.getValue(Config.SOME_VAR);
His argument against my design is that if any key needs to change or be made obsolete, all instances of the key will have to be hunted down and changed at many places in the source code, whereas in his design everything is managed from 1 file. Also, type safety poses a problem, as Integer.parseInt() from a String returned from getValue() might crash, whereas in his method, the return type is fixed.
Any comments regarding the above? Thanks.
What is so difficult to maintain about his solution? Unless the config format changes, I don't foresee any maintenance (though surprises are always possible). If maintenance is necessary, I think this solution will make it easier, as all the relevant parsing (including string->int, etc.) is in one place.
And his has far better compile-time checking. If you remove a method, the compiler will tell you. If you assign the return value to the wrong type, the compiler will tell you.
Is it correct to assume that since you are maintaining the data in configuration files, the values for these variables are fixed. If so could you not use ENUM?
public enum Data {
RED("red"),
WHITE("white");
private String color;
private Data(String color){
this.color = color;
}
public String getColor(){return color;}
}
You could even manage to use different data types if you try to think over the design a little more.
I tend to use a framework such as Apache Commons Configuration which provides typed access to properties. For example, if you have an int property you can use:
int number = config.getInteger(INT_PROPERTY);
You should define all your keys in one place so that if you decide to change the key, you only have to change it in one place. For example:
public static final String DB_PASSWORD = "db_connection_password";
To get the property use:
String password = config.get(DB_PASSWORD);
If you decide to change the key to db_passwd
, you only have to change the value of the DB_PASSWORD
constant. The rest of your code stays the same.
精彩评论