How badly considered the creation of censuses with a sufficiently large number of counters and abstract methods

I have the following listing:

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

I want to add three more abstract methods. It is considered good that enumeration contains a large number of public methods? Maybe I should only create a class in this case?

+4
source share
2 answers

Why not just use a constructor, for example:

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

Then you can add as many arguments and methods as you want, but without overriding them personally for each value (just pass it in the constructor).

+14
source

. .

Java enum : , 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);
    }
}
+3

All Articles