[英]List of enum vs. class of booleans
For now, I have a class with fields. 现在,我有一个有字段的课。
@Entity
public class Fuel {
@Id @GeneratedValue
private Long id;
private boolean diesel;
private boolean gasoline;
private boolean etanhol;
private boolean cng;
private boolean electric;
public Fuel() {
// this form used by Hibernate
}
public List<String> getDeclaredFields() {
List<String> fieldList = new ArrayList<String>();
for(Field field : Fuel.class.getDeclaredFields()){
if(!field.getName().contains("_") && !field.getName().equals("id") && !field.getName().equals("serialVersionUID") ) {
fieldList.add(field.getName());
}
Collections.sort(fieldList);
}
return fieldList;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public boolean isDiesel() {
return diesel;
}
public void setDiesel(boolean diesel) {
this.diesel = diesel;
}
public boolean isGasoline() {
return gasoline;
}
public void setGasoline(boolean gasoline) {
this.gasoline = gasoline;
}
public boolean isEtanhol() {
return etanhol;
}
public void setEtanhol(boolean etanhol) {
this.etanhol = etanhol;
}
public boolean isCng() {
return cng;
}
public void setCng(boolean cng) {
this.cng = cng;
}
public boolean isElectric() {
return electric;
}
public void setElectric(boolean electric) {
this.electric = electric;
}
}
I think it makes sense, but when I asked another question (maybe a stupid example since there can only be either automatic or manual gearbox) https://stackoverflow.com/questions/11747644/selectonemenu-from-declared-fields-list-in-pojo , a user recommend me to use enums instead. 我认为这是有道理的,但当我问另一个问题时(可能是一个愚蠢的例子,因为只能有自动或手动变速箱) https://stackoverflow.com/questions/11747644/selectonemenu-from-declared-fields-list- in-pojo ,用户建议我使用枚举。 Like this way:
像这样:
public enum Fuel {
DIESEL("diesel"),
GASOLINE("gasoline"),
ETANHOL("etanhol"),
CNG("cng"),
ELECTRIC("electric");
private String label;
private Fuel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
However, since there exists hybrids on the market (like Toyota Prius) the parent class would implement the boolean class at this way: 但是,由于市场上存在混合动力车(如丰田普锐斯),父类将以这种方式实现布尔类:
private Fuel fuel = new Fuel();
and if using enumerated list at this way: 如果以这种方式使用枚举列表:
private List<Fuel> fuelList = new ArrayList<Fuel>();
What is the best practice? 什么是最佳做法? Keep in mind that I might have 100 different fuels (just for example =).
请记住,我可能有100种不同的燃料(例如=)。 Do not forget that it is an entity and hence persisted in a database.
不要忘记它是一个实体,因此持久存储在数据库中。
Thanks in advance =) 在此先感谢=)
It sounds to me like you want an EnumSet, yes, definitely over a bunch of bool's. 听起来像你想要一个EnumSet,是的,绝对超过了一堆bool。
This reminds me a lot of the design patterns for flags and I recently posted an SO question on exactly that: Proper design pattern for passing flags to an object 这让我想起了很多标志的设计模式,我最近发布了一个SO问题: 正确的设计模式,用于将标志传递给对象
This supports having 100 different fuel types easily. 这支持容易地拥有100种不同的燃料类型。 However it doesn't support a car using 100 different fuel types simultaneously easily.
然而,它不支持同时使用100种不同燃料类型的汽车。 But that to me sounds perfectly fine - it would be very hard to build such a car and this is perfectly reflected in the programmatic complexity of coding this :) (Unless of course it really was just supporting all corn-based fuels - in which you might prefer a polymorphic pattern.)
但对我来说这听起来非常好 - 构建这样的汽车非常困难,这完全反映在编码这个程序的复杂性:)(除非当然它真的只是支持所有基于玉米的燃料 - 你在其中可能更喜欢多态模式。)
You should definetly use enums. 你应该绝对使用枚举。
Image you want to get the fuel-type of an object. 想要获得物体燃料类型的图像。
If you would use bools you would end up with something like this: 如果你使用bool,你最终会得到这样的东西:
if (myClass.IsGasoline())
else if (myClass.IsOtherFuel())
else if
...
If you use enums you can simply do something like: 如果您使用枚举,您可以简单地执行以下操作:
Fuel fuel = myClass.GetFuelType()
(This is just pseudo-code ;)) (这只是伪代码;))
If the number of hybrids is low, and I guess it will be better to use Enums, and include hybrids as a different case. 如果混合动力车的数量很少,我想最好使用Enums,并将混合动力车作为一个不同的案例。 Otherwise you will have to manage the logic in a way that can be cumbersome, as when you set a certain Fuel to true you, most likely, will have also to set to false the current one set to true.
否则,您将必须以一种可能很麻烦的方式管理逻辑,因为当您将某个Fuel设置为true时,您很可能也将设置为false,将当前设置为true。 I am saying this as you have setters for your fuel categories and you don't only define at construction.
我这样说是因为你有燃料类别的定位器,你不仅要在施工中定义。
EDIT: the way on how to ask for the type of fuel you are using would also be an argument in favor of enums. 编辑:如何询问您正在使用的燃料类型的方式也将支持枚举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.