Java 哪种设计模式用于合并两种方法?它们在一个地方不同
我想知道在我的案例中应该使用哪种设计模式:我有两个端点,它们使用两种服务方法,每种方法都非常相似,它们只是在从同一服务调用一些不同的方法时有所不同: 我的第一个服务方法(由端点1调用):Java 哪种设计模式用于合并两种方法?它们在一个地方不同,java,spring-boot,design-patterns,spring-webflux,Java,Spring Boot,Design Patterns,Spring Webflux,我想知道在我的案例中应该使用哪种设计模式:我有两个端点,它们使用两种服务方法,每种方法都非常相似,它们只是在从同一服务调用一些不同的方法时有所不同: 我的第一个服务方法(由端点1调用): private Mono<Boolean> deleteAAA( List<SecuritySet> securitySets, boolean deleteRecoveryPoints) { return Flux.fromIterable(securi
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);
}
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);
}
我可以传递给这些方法
deleteAA
和deleteBB
一个类似Type-Type
的参数,以某种方式区分这些方法的调用。将这两种方法合并为一种方法的最佳方法是什么?提取出不同的方法。可以使用lambda表达式(或方法引用)将函数作为参数传递
把变化的东西抽象出来。可以使用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
}
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);
}