如果使用枚举,如何过多清除代码

Ish*_*rav 3 java code-cleanup java-8

我有以下代码要编写。它采用一种枚举类型并返回其他枚举值。如何删除代码中太多(如果没有)条件并使其干净?

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;
}
Run Code Online (Sandbox Code Playgroud)

Ond*_* K. 8

您可以Map按照他人的建议使用a ,但是如果您认为合适,我个人可以使用委托。在您的QuestionTypeInfo接口中,声明一个getQuestionType返回QuestionType枚举实例的抽象方法,并在其所有实现中使用适当的值覆盖它。

interface QuestionTypeInfo {
    QuestionType getQuestionType();
}

enum OpenEndedTextQuestionTypeInfo implements QuestionTypeInfo {
    @Override
    public QuestionType getQuestionType() {
        return QuestionType.OPEN_ENDED;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

private static QuestionType parseQuestionType(QuestionTypeInfo questionTypeInfo) {
        return questionTypeInfo.getQuestionType();
}
Run Code Online (Sandbox Code Playgroud)

  • 如果可以对基本接口进行这种重构,那么这就是要走的路,每一个其他解决方案都将打破OOP,因为需要知道基本接口的每个子类型。这样,实现类必须自行决定“ QuestionType”是什么。+1 (2认同)