簡體   English   中英

將代碼重構提取為方法

[英]Code Refactoring Extract To a Method

友人

我下面有兩個拋出相同ServiceException的重載方法

private ModResponse updateNDelTerms(GlobalTermDeleteType item, boolean isGlobal)
    {
        StdTerm stdTerm = getTerm(item.getstdTermId());
        if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
        {
            throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
                    CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
        }

        if (stdTerm.isGlobal() && !isGlobal)
        {
            throw new ServiceException(
                    "Global Term can not be updated: incorrect URL.",
                    CANNOT_UPDATE_GLOBAL_TERM, INCORRECT_URL);
        }

        if (stdTerm.isLocked() != null && stdTerm.isLocked())
        {
            throw new ServiceException("Global Term can not be updated: stdTerm is locked.",
                    CANNOT_UPDATE_GLOBAL_TERM, TERM_LOCKED);
        }

        return updateNDel(item, stdTerm);
    }

第二種方法是

public ItemResponse<List<stdTermItemType>> copyTerm(
                                        BigInteger stdTermId, boolean isGlobal,
            boolean isFalse)
    {
        StdTerm stdTerm = getTerm(stdTermId);

        if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
        {
            throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
                    CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
        }

        if (stdTerm.isGlobal() && !isGlobal)
        {
            throw new ServiceException(
                    "Global Term can not be updated: incorrect URL.",
                    CANNOT_COPY_GLOBAL_TERM, INCORRECT_URL);
        }

        if (stdTerm.isLocked() != null && stdTerm.isLocked())
        {
            throw new ServiceException("Rate sheet term can not be updated: stdTerm is locked.",
                    CANNOT_COPY_GLOBAL_TERM, TERM_LOCKED);
        }
        return copyGlobleTerm(stdTerm, pasteTermObj, isFalse);
    }

我正在考慮對這兩種方法進行重構,並使用將通用代碼提取到方法中,但是由於CANNOT_COPY_GLOBAL_TERMCANNOT_UPDATE_GLOBAL_TERM的差異,這兩種方法都是唯一的,因此無法實現對方法的提取。 請提出建議。

將不同的事物傳遞到提取的方法中。

如果您在JDK 8上運行,也可以將這些方法轉換為lambda,並將其傳遞給它們。使用更具功能性的樣式。

private StdTerm retrieveStdTerm(BigInteger stdTermId, boolean isGlobal, String errorTerm)
{
    StdTerm stdTerm = getTerm(BigInteger stdTermId);
    if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
    {
        throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
                CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
    }
    if (stdTerm.isGlobal() && !isGlobal)
    {
        throw new ServiceException(
                "Global Term can not be updated: incorrect URL.",
                errorTerm, INCORRECT_URL);
    }
    if (stdTerm.isLocked() != null && stdTerm.isLocked())
    {
        throw new ServiceException("Global Term can not be updated: stdTerm is locked.",
                errorTerm, TERM_LOCKED);
    }
    return stdTerm;
}

private ModResponse updateNDelTerms(GlobalTermDeleteType item, boolean isGlobal)
{
    StdTerm stdTerm = retrieveStdTerm(item.getstdTermId(), isGlobal,
            CANNOT_DELETE_GLOBAL_TERM);
    return updateNDel(item, stdTerm);
}

public ItemResponse<List<stdTermItemType>> copyTerm(BigInteger stdTermId, boolean isGlobal,
        boolean isFalse)
{
    StdTerm stdTerm = retrieveStdTerm(stdTermId, isGlobal, CANNOT_COPY_GLOBAL_TERM);
    return copyGlobleTerm(stdTerm, pasteTermObj, isFalse);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM