No, you're doing it right. And it's actually fairly maintenance-free.
Using Java Enums would be slightly preferable - i.e.
public class PropertiesWrapper {
private final Properties props;
...
public String get(MyEnum key) { return props.get(key.toString());
}
The reason is that even if you make that String a constant, you can never change it without recompiling all code which uses the constant - because the compiler will replace the constant with "appName" at compile-time.
If you use enums and remove an enum constant, code will still need recompiling, but it won't appear to be fine when it's actually now asking for the wrong thing. Also, by using toString()
instead of name()
to get the property name, you are free to override toString()
in your enum to return something different than the constant name.
The down-side of using enums is that the system can't look up anything which was not known at compile-time without some alternate way to access the Properties
.