java - 如果使用枚举,如何清理代码太多

标签 java java-8 code-cleanup

我要编写以下代码。它采用一种枚举类型并返回其他枚举值。如何去除代码中过多的if else条件并使其干净?

private static QuestionType parseQuestionType(QuestionTypeInfo questionTypeInfo) {
        if (questionTypeInfo instanceof OpenEndedTextQuestionTypeInfo) {
            return QuestionType.OPEN_ENDED;
        } else if (questionTypeInfo instanceof MultiChoiceQuestionTypeInfo) {
            return QuestionType.MULTI_CHOICE;
        } else if (questionTypeInfo instanceof MatrixSinglePerRowQuestionTypeInfo) {
            return QuestionType.MATRIX_SINGLE_PER_ROW;
        } else if (questionTypeInfo instanceof OpenEndedTextQuestionTypeInfo) {
            return QuestionType.OPEN_ENDED;
        } else if (questionTypeInfo instanceof MatrixMultiPerRowQuestionTypeInfo) {
            return QuestionType.MATRIX_MULTI_PER_ROW;
        } else if (questionTypeInfo instanceof MatrixSideBySideQuestionTypeInfo) {
            return QuestionType.MATRIX_SIDE_BY_SIDE;
        } else if (questionTypeInfo instanceof MatrixSpreadSheetQuestionTypeInfo) {
            return QuestionType.MATRIX_SPREAD_SHEET;
        } else if (questionTypeInfo instanceof DataListQuestionTypeInfo) {
            return QuestionType.DATA_LIST;
        } else if (questionTypeInfo instanceof FileUploadQuestionTypeInfo) {
            return QuestionType.FILE_UPLOAD;
        } else if (questionTypeInfo instanceof InteractiveSlidingScaleQuestionTypeInfo) {
            return QuestionType.INTERACTIVE_SLIDING_SCALE;
        } else if (questionTypeInfo instanceof NetPromoterQuestionTypeInfo) {
            return QuestionType.NET_PROMOTER;
        } else if (questionTypeInfo instanceof RankOrderQuestionTypeInfo) {
            return QuestionType.RANK_ORDER;
        } else if (questionTypeInfo instanceof PresentationHeaderQuestionTypeInfo) {
            return QuestionType.PRESENTATION_HEADER;
        } else if (questionTypeInfo instanceof PresentationHtmlQuestionTypeInfo) {
            return QuestionType.PRESENTATION_HTML;
        } else if (questionTypeInfo instanceof AutoIncrementQuestionTypeInfo) {
            return QuestionType.AUTO_INCREMENT;
        } else if (questionTypeInfo instanceof SingleChoiceQuestionTypeInfo) {
            return QuestionType.SINGLE_CHOICE;
        }

        return null;
}

最佳答案

您可以按照其他人的建议使用 Map,但如果对您的情况有意义,我个人会使用委托(delegate)。在您的 QuestionTypeInfo 接口(interface)中,声明一个抽象方法 getQuestionType 返回 QuestionType 枚举的实例,并在其所有实现中使用适当的值覆盖它.

interface QuestionTypeInfo {
    QuestionType getQuestionType();
}

enum OpenEndedTextQuestionTypeInfo implements QuestionTypeInfo {
    @Override
    public QuestionType getQuestionType() {
        return QuestionType.OPEN_ENDED;
    }
}

然后,在 parseQuestionType 方法中,只需使用:

private static QuestionType parseQuestionType(QuestionTypeInfo questionTypeInfo) {
        return questionTypeInfo.getQuestionType();
}

关于java - 如果使用枚举,如何清理代码太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809769/

相关文章:

.net - 去除挂代码的工具

testing - 清洁代码、测试和重用性说明

java - rCaller 生成一个空图像和空图

java SHOUTcast : i have error with BasicPlayer

java - 如何在 Java 8 中创建阻塞后台加载程序?

java-8 - 在 Java8 中对 else 条件使用相同的流

java - 如何从 LocalDate 和 LocalDateTime 中提取纪元?

c++ - 如何使这个 C++ 代码更 DRY?

java - 显示 jTable 中两个给定日期之间的元素

java - 如何在hazelcast配置中添加成员ip