dimanche 19 juillet 2020

Which design pattern for merging 2 methods they differ in one place

I wonder which design pattern should I use in my case: I have 2 endpoints that use 2 service methods, each very similar, they differ only in invoking some different methods from the same service:

My first service method (invoked by endpoint 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);
                }

The second endpoint uses very similar method that differs in protectionCommandService.sendUnprotected (deleteBBB).

My secondservice method (invoked by endpoint 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);
                }

I can pass to these methods deleteAAA and deleteBBB a parameter like Type type to somehow differentiate between the invoking of these methods. What would be the best way to merge these 2 methods into one method?

Aucun commentaire:

Enregistrer un commentaire