Choice between enum and final class for storage of options
-
I'm developing a Java 8 application that I need to use some construction. The constructions may be different, unite them now, that there is some idea of the construction (now this is a transfer element) and the key and importance of the construction. Plans to be added
ArrayList
♪Now I have a choice, whether to use a general interface to keep the lines, or to try to use it.
final class
♪Specifically, Java, I'm just beginning to be serious, so the question may be silly, but the search for the Russian and English Network didn't make me clear.
Indicating the example (extensive) of what has now been accomplished:
public interface Option { public String getKey(); public String getValue(); }
public enum Option1 implements Option {
OPTION1_VALUE_1("one"),
OPTION1_VALUE_2("two");
private final String key;
private final String value;
Option1(String val) {
this.key = "key1";
this.value = val;
}
@Override
public String getKey() {
return this.key;
}
@Override
public String getValue() {
return this.value;
}
}public enum Option2 implements Option {
OPTION2_VALUE_1("aaa"),
OPTION2_VALUE_2("bbb");
private final String key;
private final String value;
Option2(String val) {
this.key = "key2";
this.value = val;
}
@Override
public String getKey() {
return this.key;
}
@Override
public String getValue() {
return this.value;
}
}
Then I plan to use these "options" somehow:
List<Option> options = new ArrayList<Option>;
options.add(Option1.OPTION1_VALUE_1);
options.add(Option2.OPTION2_VALUE_1);
options.add(Option2.OPTION2_VALUE_2);
// А теперь, например, обходим список и выводим value
for (Option opt : options) {
System.out.println(opt.getValue());
}
That's what I'm using.
I do not like the need for each enum to redesign the functions.
getKey()
andgetValue()
♪ Follow.enum
Java doesn't allow it.The homing led me to the article, https://en.wikipedia.org/wiki/Constant_interface "In Wikipedia, where did I draw the idea of doing this through
final class
♪Realization is like this:
public class Option {
protected final String key;
public getKey() {
return this.key;
}
}public final class Option1 extends Option {
Option() {
this.key = "key1";
}
public static final String OPTION1_VALUE_1 = "one";
public static final String OPTION1_VALUE_2 = "two";
}public final class Option2 extends Option {
Option() {
this.key = "key2";
}
public static final String OPTION2_VALUE_1 = "aaa";
public static final String OPTION2_VALUE_2 = "bbb";
}
The code was rolling right here, without a panty, just to give the idea. Accordingly, in this exercise, I refuse the method
getValue()
and get value through the class member.In fact, questions:
- Is there any way to avoid the needdetermination?
getKey()
andgetValue()
in the first option of implementation (through
enum's? - The extent to which the first implementation is appropriate, from a position above
experienced developers? - Is the idea of a second implementation viable (through)
final class
? Better.
Is it more than using enum's? Personally, I think for these purposes.
better use the list, but because of lack of experience
I highly doubt that decision.
- Is there any way to avoid the needdetermination?
-
The first implementation can be replaced by a better one. Enum can define its design and methods. Then there's no need to create an interface, etc. I think it'll be the best solution.
public enum Option { OPTION_1("key 1", "value 1"), OPTION_2("key 2", "value 2");
private String key; private String value; Option(String key, String value) { this.key = key; this.value = value; } public String getKey() { return key; } public String getValue() { return value; }
}
UPDATE
About use. If all options are required, that is, a standard method
values()
:for (Option op : Option.values()) {
System.out.println(op.getKey() + " - " + op.getValue());
}
If only a part of the Opium is to be operated, use it. https://docs.oracle.com/javase/7/docs/api/java/util/EnumSet.html which provides a compact and rapid data structure through the internal implementation of a beat vector.