I have two versions of an API with similar but slightly different response structure. We are currently using V1 of the API and will gradually move to V2 soon. The switch has to done using a feature flag that gets triggered remotely.
Our class look like this,
@RequiredArgsConstructor
class ServiceA {
private final ExtService<ResponseV1> extService;
public void someMethod() {
ResponseV1 response = extService.get(someId);
response.updateSomeFields();
extService.save(response);
......
}
}
With feature flag, the code will look something like this.
@RequiredArgsConstructor
class ServiceA {
private final ExtService<ResponseV1> extServicev1;
private final ExtService<ResponseV2> extServicev2;
private final FeatureFlagService ffService;
public void someMethod() {
if(ffService.useV2()) {
ResponseV2 response = extService2.get(someId);
response.updateSomeFields();
extService2.save(response);
} else {
ResponseV1 response = extService1.get(someId);
response.updateSomeFields();
extService1.save(response);
}
......
}
}
Is there any way where we can implement this where the version switching using feature flag can be abstracted? Is there any design pattern available to solve similar problem?
-Edit--------------------------------------------------- Apologies if my question was unclear. What I meant was if there any way where we can abstract the logic to switch between versions and just execute the steps without knowing which version is getting used.