如何重构switch语句的此重复代码片段

我正在编写一些CRUD应用程序的代码,该页面显示数据库表中的数据,并且应该与两个非常相似的表连接(由于历史原因或时间太长,这两个非常相似的表无法优化为一个表)。我确实使用switch语句和Java 8函数进行了优化,但是Intellij IDEA在两个不知道如何解决它的switch语句之间给了我重复代码片段警告。

我不是要优化设计,而是重构重复的代码片段并修复此警告。

我有一种search方法,用于从数据库中搜索数据,并通过TaxCustomerType枚举使用来自不同表的单独逻辑:

    public PageUtils<IndividualTaxactorVO> search(TaxCustomerType type,IndividualTaxactorQuery query,UserDO principal) {
        //... ignore for conciseness
        Function<actorCondition,Integer> counter;
        Function<actorCondition,Collection<IndividualTaxactorVO>> searcher;
        switch (type) {
            case INDIVIDUAL: {
                counter = individualTaxactordao::countIndividual;
                searcher = individualTaxactordao::searchIndividual;
                break;
            }
            case CORPORATE: {
                counter = individualTaxactordao::countCorporate;
                searcher = individualTaxactordao::searchCorporate;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }
        int total = counter.apply(condition);
        if (total == 0) {
            return PageUtils.empty();
        }
        Collection<IndividualTaxactorVO> list = searcher.apply(condition);
        return new PageUtils<>(list,total);
    }

我还有一个link方法可以插入表中并进行一些检查,也可以使用TaxCustomerType枚举使用单独的逻辑:

    public void link(TaxCustomerType type,String customerId,UserDO principal) {
        Function<String,IdAndTaxCodeAndDisabledOnly> findById;
        BiFunction<String,String,Integer> existsCounter;
        switch (type){
            case INDIVIDUAL: {
                findById = individualCustomerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxactordao::countCorporateByCompanyIdAndTaxCode;
                break;
            }
            case CORPORATE: {
                findById = customerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxactordao::countIndividualByCompanyIdAndTaxCode;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }
        IdAndTaxCodeAndDisabledOnly customer = findById.apply(customerId);
        if (customer == null) {
            throw new ApplicationRuntimeException(HttpStatus.NOT_FOUND,"客户不存在");
        }
        if(customer.isDisabled()){
            throw new ApplicationRuntimeException(HttpStatus.FAILED_DEPENDENCY,"客户已禁用");
        }
        int exists=existsCounter.apply(principal.getcompanyid(),customerId);
        if(exists>0){
            throw new ApplicationRuntimeException(HttpStatus.CONFLICT,"已是智能个税客户,不可重复添加。");
        }
        // insert into table ... ignore for conciseness
    }

绝对重复的代码片段行是:

search方法中:

        switch (type) {
            case INDIVIDUAL: {
                counter = individualTaxactordao::countIndividual;
                searcher = individualTaxactordao::searchIndividual;
                break;
            }
            case CORPORATE: {
                counter = individualTaxactordao::countCorporate;
                searcher = individualTaxactordao::searchCorporate;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

并使用link方法:

        switch (type){
            case INDIVIDUAL: {
                findById = individualCustomerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxactordao::countCorporateByCompanyIdAndTaxCode;
                break;
            }
            case CORPORATE: {
                findById = customerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxactordao::countIndividualByCompanyIdAndTaxCode;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

我认为这两个代码段具有绝对不同的逻辑,但具有相同的switch模式,我无法忍受IDE警告,请帮助我修复它。

zhzh3456789 回答:如何重构switch语句的此重复代码片段

您比较的代码片段严格来说并不是重复的。 它们看起来很相似,但截然不同。就可读性而言,没有最佳的方法来优化此方法(假设您希望使用switch es保留基本结构)。

此外,您的switch“返回”两个结果,它们的类型和值都非常不同。如果不同,则可以提取一个方法并返回Pair的{​​{1}}(Apache库),但是由于Function是完全不同的(甚至是一个Function),这是不可能的。

我会坚持使用您拥有的代码。

,

TreffnonX的想法,我制作了“返回”函数的包装器类。

private static class SearchFunctions{
        Function<ActorCondition,Integer> counter;
        Function<ActorCondition,Collection<IndividualTaxActorVO>> searcher;
        //ignore ...
}
private static class LinkFunctions{
        Function<String,IdAndTaxCodeAndDisabledOnly> findById;
        BiFunction<String,String,Integer> existsCounter;
}

search方法中:

        switch (type) {
            case INDIVIDUAL: {
                functions=new SearchFunctions(individualTaxActorDao::countIndividual,individualTaxActorDao::searchIndividual);
                break;
            }
            case CORPORATE: {
                functions=new SearchFunctions(individualTaxActorDao::countCorporate,individualTaxActorDao::searchCorporate);
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

并使用link方法:

        switch (type) {
            case INDIVIDUAL: {
                functions=new LinkFunctions(individualCustomerService::findTaxCodeAndDisabledById,individualTaxActorDao::countCorporateByCompanyIdAndTaxCode);
                break;
            }
            case CORPORATE: {
                functions=new LinkFunctions(customerService::findTaxCodeAndDisabledById,individualTaxActorDao::countIndividualByCompanyIdAndTaxCode);
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

是的,可以!现在没有重复的代码片段警告!

但是我坚持认为成本太高了!应该做出决定。

本文链接:https://www.f2er.com/3140248.html

大家都在问