使用足够多的枚举器和抽象方法创建枚举有多糟糕
How bad is it considered to create enums with large enough amount of enumerators and abstract methods
我有以下枚举:
public enum RuleItem {
MORE_THAN(1) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.moreThan");
}
},
LESS_THAN(2) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.lessThan");
}
},
MORE_OR_EQUAL(3) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.moreOrEqual");
}
},
//...
INTERVAL_WITH_BOUNDS_INCLUDED(27) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.intervalWithBounds");
}
};
protected String getRuleStringRepresentation(String resourceName) {
Locale locale = FacesContext.getCurrentInstance().getViewRoot()
.getLocale();
String resourceString;
try {
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME,
locale);
resourceString = bundle.getString(resourceName);
} catch (MissingResourceException e) {
return null;
}
return resourceString;
}
public abstract String getStringRepresentation();
}
我想再添加三个抽象方法。枚举包含大量 public 方法是否被认为是好的?在那种情况下,也许我应该只创建一个 class?
为什么不简单地使用构造函数,例如:
public enum RuleItem {
MORE_THAN(1, "rulesName.moreThan"),
LESS_THAN(2, "rulesName.lessThan"),
MORE_OR_EQUAL(3, "rulesName.moreOrEqual");
private int value;
private String representation;
private RuleItem(int value, String representation) {
this.value = value;
this.representation = representation;
}
public String getStringRepresentation() {
return representation;
}
}
然后您可以根据需要添加任意数量的参数和方法,但无需为每个值单独覆盖它(只需将其传递到构造函数中)。
我没有发现使用多个 public 方法进行枚举有任何问题。枚举项本身就是对象。
我喜欢 Java enum
实现只是因为这个原因:你有对象而不是像 C 或 C# 中那样的裸值。
无论如何,枚举项应该是不可变的对象,它们可以将进一步的阐述委托给其他对象。
public interface RuleExecutor {
public void execute(int param1, int param2);
}
public enum RuleItem {
MORE_THAN("rulesName.moreThan", new MoreThanExecutor()),
LESS_THAN("rulesName.lessThan" , new LessThanExecutor()),
MORE_OR_EQUAL("rulesName.moreOrEqual", new MoreOrEqualExecutor());
private String representation;
private RuleExecutor executor;
private RuleItem(String representation, RuleExecutor executor) {
this.representation = representation;
this.executor = executor;
}
public String getStringRepresentation() {
return getRuleStringRepresentation(representation);
}
public void execute(int param1, int param2) {
this.executor.execute(param1, param2);
}
}
我有以下枚举:
public enum RuleItem {
MORE_THAN(1) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.moreThan");
}
},
LESS_THAN(2) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.lessThan");
}
},
MORE_OR_EQUAL(3) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.moreOrEqual");
}
},
//...
INTERVAL_WITH_BOUNDS_INCLUDED(27) {
@Override
public String getStringRepresentation() {
return getRuleStringRepresentation("rulesName.intervalWithBounds");
}
};
protected String getRuleStringRepresentation(String resourceName) {
Locale locale = FacesContext.getCurrentInstance().getViewRoot()
.getLocale();
String resourceString;
try {
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME,
locale);
resourceString = bundle.getString(resourceName);
} catch (MissingResourceException e) {
return null;
}
return resourceString;
}
public abstract String getStringRepresentation();
}
我想再添加三个抽象方法。枚举包含大量 public 方法是否被认为是好的?在那种情况下,也许我应该只创建一个 class?
为什么不简单地使用构造函数,例如:
public enum RuleItem {
MORE_THAN(1, "rulesName.moreThan"),
LESS_THAN(2, "rulesName.lessThan"),
MORE_OR_EQUAL(3, "rulesName.moreOrEqual");
private int value;
private String representation;
private RuleItem(int value, String representation) {
this.value = value;
this.representation = representation;
}
public String getStringRepresentation() {
return representation;
}
}
然后您可以根据需要添加任意数量的参数和方法,但无需为每个值单独覆盖它(只需将其传递到构造函数中)。
我没有发现使用多个 public 方法进行枚举有任何问题。枚举项本身就是对象。
我喜欢 Java enum
实现只是因为这个原因:你有对象而不是像 C 或 C# 中那样的裸值。
无论如何,枚举项应该是不可变的对象,它们可以将进一步的阐述委托给其他对象。
public interface RuleExecutor {
public void execute(int param1, int param2);
}
public enum RuleItem {
MORE_THAN("rulesName.moreThan", new MoreThanExecutor()),
LESS_THAN("rulesName.lessThan" , new LessThanExecutor()),
MORE_OR_EQUAL("rulesName.moreOrEqual", new MoreOrEqualExecutor());
private String representation;
private RuleExecutor executor;
private RuleItem(String representation, RuleExecutor executor) {
this.representation = representation;
this.executor = executor;
}
public String getStringRepresentation() {
return getRuleStringRepresentation(representation);
}
public void execute(int param1, int param2) {
this.executor.execute(param1, param2);
}
}