patternjavaMinor
Initializing complex constant in Constants interface
Viewed 0 times
constantinitializinginterfaceconstantscomplex
Problem
I'm being forced to use the common anti-pattern of a
Unfortunately, I can't use static initializers in an interface. So after much frustration, I finally have my implementation as follows:
I hate the fact that I've made an anonymous extension like this and it makes me cringe. Are there any better techniques I can use to accomplish this? Changing the
public interface Constants which many classes in this project implement. Long story short, I need to have a List constant which is pre-populated with values. Normally I would do this like so:public static final List MY_CONSTANT = new ArrayList();
static {
MY_CONSTANT.add("foo");
MY_CONSTANT.add("bar");
// ...
}Unfortunately, I can't use static initializers in an interface. So after much frustration, I finally have my implementation as follows:
public static final List MY_CONSTANT = new ArrayList() {
private static final long serialVersionUID = 1898990046107150596L;
{
add("foo");
add("bar");
// ...
}
}I hate the fact that I've made an anonymous extension like this and it makes me cringe. Are there any better techniques I can use to accomplish this? Changing the
Constants file to a class instead of an interface isn't an option. As I said, I'm just retouching a large, pre-existing code base.Solution
If you only need constant of
which creates immutable list instance with passed values.
In cases when more complex object needs to be initialised I like to use static methods which create my constant:
In your case you won't be able to declare
Hope this helps...
java.util.List type you can use static method java.util.Arrays.asList:import static java.util.Arrays.asList;
...
public static final List MY_CONSTANT = asList("foo", "bar", "baz");which creates immutable list instance with passed values.
In cases when more complex object needs to be initialised I like to use static methods which create my constant:
public static final SomeComplexObject constant = createMyConstant();
private static SomeComplexObject createMyConstant() {
SomeComplexObject constant = new SomeComplexObject();
constant.setProp1(...);
constant.setProp2(...);
...
return makeItSomehowImmutable(constant);
}In your case you won't be able to declare
private static method inside interface, so the only thing I find reasonable is to create separate utility class with static methods which create constants, declare your method there and reuse it just like with java.util.Arrays.asList method.Hope this helps...
Code Snippets
import static java.util.Arrays.asList;
...
public static final List<String> MY_CONSTANT = asList("foo", "bar", "baz");public static final SomeComplexObject constant = createMyConstant();
private static SomeComplexObject createMyConstant() {
SomeComplexObject constant = new SomeComplexObject();
constant.setProp1(...);
constant.setProp2(...);
...
return makeItSomehowImmutable(constant);
}Context
StackExchange Code Review Q#30380, answer score: 7
Revisions (0)
No revisions yet.