【问题标题】:Which design pattern for merging 2 methods they differ in one place哪种设计模式用于合并它们在一个地方不同的 2 种方法
【发布时间】:2020-07-19 17:32:05
【问题描述】:

我想知道在我的情况下应该使用哪种设计模式:我有 2 个端点,它们使用 2 个服务方法,每个都非常相似,它们的区别仅在于从同一个服务调用一些不同的方法:

我的第一个服务方法(由端点 1 调用):

private Mono<Boolean> deleteAAA(
        List<SecuritySet> securitySets, boolean deleteRecoveryPoints) {
        return Flux.fromIterable(securitySets)
            .flatMap(
                protectionSet ->
                protectorRepository
                ...
                .flatMap(
                    protector ->
                    Mono.zip(
                        //some code)
                        .flatMap(
                            tuple ->
                            securityCommandService.sendUnprotectedAAA( // -> DIFFERENCE
                                tuple.getT1()))
                        .doOnNext(
                            securitySetId ->
                            subscriptionResourceService.cancelSubscriptionResources(
                                securitySet, protector))
                        .doOnNext(
                            //some code)
                            .map(
                                protectionSetId ->
                                createSecurityObject(securitySet, protector))
                            .doOnNext(schedulerService::deleteSecurity)))
                    .collectList()
                    .thenReturn(true);
                }

第二个端点使用与protectionCommandService.sendUnprotected 不同的非常相似的方法 (deleteBBB)。

我的 secondservice 方法(由端点 2 调用):

private Mono<Boolean> deleteBBB(
        List<SecuritySet> securitySets, boolean deleteRecoveryPoints) {
        return Flux.fromIterable(securitySets)
            .flatMap(
                protectionSet ->
                protectorRepository
                ...
                .flatMap(
                    protector ->
                    Mono.zip(
                        //some code)
                        .flatMap(
                            tuple ->
                            securityCommandService.sendUnprotectedBBB( // -> DIFFERENCE
                                tuple.getT1()))
                        .doOnNext(
                            securitySetId ->
                            subscriptionResourceService.cancelSubscriptionResources(
                                securitySet, protector))
                        .doOnNext(
                            //some code)
                            .map(
                                protectionSetId ->
                                createSecurityObject(securitySet, protector))
                            .doOnNext(schedulerService::deleteSecurity)))
                    .collectList()
                    .thenReturn(true);
                }

我可以将Type type 之类的参数传递给deleteAAAdeleteBBB 这些方法,以某种方式区分这些方法的调用。将这两种方法合并为一种方法的最佳方法是什么?

【问题讨论】:

    标签: java spring-boot design-patterns spring-webflux


    【解决方案1】:

    抽象出变化的内容。您可以使用 lambda 表达式(或方法引用)将函数作为参数传递。

    private Mono <Boolean> delete(List <SecuritySet> securitySets, Function<Tuple, List<Id>> unprotecedAAACall,
               boolean deleteRecoveryPoints) {
        return Flux.fromIterable(securitySets)
                .....//rest all same
                .flatMap(unprotecedAAACall)//function is passed in 
                ..... //rest all same       
    }
    

    在上面的代码中,我们传递了一个Function,它将一个元组映射到something。为了演示,我将该类型命名为Id

    称之为

    private Mono <Boolean> deleteAAA(List <SecuritySet> securitySets, boolean deleteRecoveryPoints) {
        return delete(securitySets, tuple -> 
                securityCommandService.sendUnprotectedAAA(tuple.getT1()),
                deleteRecoveryPoints);
    }
    
    private Mono <Boolean> deleteBBB(List <SecuritySet> securitySets, boolean deleteRecoveryPoints) {
        return delete(securitySets, tuple -> 
                securityCommandService.sendUnprotectedBBB(tuple.getT1()),
                deleteRecoveryPoints);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2012-07-18
      • 2023-03-10
      • 2019-02-19
      • 2011-02-27
      • 2012-09-10
      相关资源
      最近更新 更多