Я могу подумать об этом.
Создать интерфейс для службы.
public interface UpdateService {
void updateStatus(UpdateStatusRequest updateStatusRequest);
}
Затем вы создадите различные реализации.
public class FooUpdateService implements UpdateService {
void updateStatus(UpdateStatusRequest updateStatusRequest) {
// foo specific logic
}
}
public class BarUpdateService implements UpdateService {
void updateStatus(UpdateStatusRequest updateStatusRequest) {
// Bar specific logic
}
}
Создайте UpdateServiceFactory
public class UpdateServiceFactory {
@Autowired
private UpdateService fooUpdateService;
@Autowired
private UpdateService fooUpdateService;
public UpdateService getUpdateService(String group) {
// Move the if-else logic here
if(APIUrl.FOO_GROUP.equals(group)){
return fooUpdateService;
}
else{
//code spefic to bar
return barUpdateService;
}
}
}
Контроллер:
@PostMapping(value = UPDATE_STATUS_API_PATH)
public Response updateStatus(@PathVariable("group") String group , @RequestBody UpdateStatusRequest updateStatusRequest, HttpServletRequest request) {
updateServiceFactory.getUpdateService(group).updateStatus(updateStatusRequest);
//common code
}