Java 使用足够多的枚举数和抽象方法创建枚举有多糟糕

Java 使用足够多的枚举数和抽象方法创建枚举有多糟糕,java,enums,Java,Enums,我有以下枚举: public enum RuleItem { MORE_THAN(1) { @Override public String getStringRepresentation() { return getRuleStringRepresentation("rulesName.moreThan"); } }, LESS_THAN(2) { @Override

我有以下枚举:

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

我想再添加三个抽象方法。enum包含大量的公共方法,这被认为是好的吗?在这种情况下,也许我应该只创建一个类?

为什么不简单地使用构造函数,比如:

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

然后,您可以添加任意数量的参数和方法,但不必为每个值单独重写(只需将其传递到构造函数中即可)。

我认为使用大量公共方法进行枚举没有任何问题。枚举项本身就是对象

我喜欢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);
    }
}

构造函数中的
表示法
应该是
字符串
而不是
int
。哎呀。。。我想这一条写得太快了。谢谢我已经相应地编辑了我的答案。