枚举列表与布尔类的列表 [英] List of enum vs. class of booleans

查看:121
本文介绍了枚举列表与布尔类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有一个带有字段的课程。

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;
    }   

}

我认为这是有道理的,但是当我问另一个问题时(可能是一个愚蠢的例子,因为只能有自动或手动变速箱) https://stackoverflow.com/questions/11747644/selectonemenu-from-declared-fields-list-in-pojo ,用户建议我改用枚举。喜欢这种方式:

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. 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>();

最佳做法是什么?请记住,我可能有100种不同的燃料(例如=)。不要忘记它是一个实体,因此持久存储在数据库中。

What is the best practice? Keep in mind that I might have 100 different fuels (just for example =). Do not forget that it is an entity and hence persisted in a database.

提前谢谢=)

推荐答案

听起来我觉得你想要一个EnumSet,是的,肯定超过了一堆bool's。

It sounds to me like you want an EnumSet, yes, definitely over a bunch of bool's.

这让我想起了很多旗帜的设计模式我最近发布了一个SO问题:将标志传递给物体的正确设计模式

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

这支持轻松拥有100种不同的燃料类型。然而,它不支持同时使用100种不同燃料类型的汽车。但对我来说这听起来非常好 - 构建这样的汽车将非常困难,这完全反映在编码的程序化复杂性:)(除非当然它真的只是支持所有基于玉米的燃料 - 你在其中可能更喜欢多态模式。)

This supports having 100 different fuel types easily. However it doesn't support a car using 100 different fuel types simultaneously easily. 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.)

这篇关于枚举列表与布尔类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆