Мне нужно выбрать правильную реализацию LocationProvider
во время выполнения. И то и другое
GoogleLocationService
и AndroidLocationService
имеют свои зависимости.
public interface LocationProvider{
Observable<Location> getLocationProvider(Looper looper);
}
@ActivityScope
public class GoogleLocationService implements LocationProvider{
...
@Inject
public GoogleLocationService (...){
}
}
@ActivityScope
public class AndroidLocationService implements LocationProvider{
...
@Inject
public AndroidLocationService (...){
}
}
@ActivityScope
public class LocationControl{
LocationProvider locationProvider;
@Inject
public LocationControl(...){
...
}
public void start(){
if(isGooglePlayServiceAvailable){
locationProvider = new GoogleLocationService(...);
} else {
locationProvider = new AndroidLocationService(...);
}
}
}
Каков наилучший подход для удовлетворения этих зависимостей с помощью Dagger 2?
Должен ли я использовать ленивые инъекции?
@ActivityScope
public class LocationControl{
@Inject
Lazy<AndroidLocationService> androidlocationService;
@Inject
Lazy<GoogleLocationService> googlelocationService;
LocationProvider locationProvider;
@Inject
public LocationControl(...){
...
}
public void start(){
if(isGooglePlayServiceAvailable){
locationProvider = googlelocationService.get();
} else {
locationProvider = androidLocationService.get();
}
}
}
Или с завода?
@ActivityScope
public class LocationControl{
private final LocationProviderFactory locationProviderFactory;
LocationProvider locationProvider;
@Inject
public LocationControl(LocationProviderFactory locationProviderFactory){
this.locationProviderFactory = locationProviderFactory;
}
public void start(){
if(isGooglePlayServiceAvailable){
locationProvider = locationProviderFactory.getLocationProvider(google);
} else {
locationProvider = locationProviderFactory.getLocationProvider(android);
}
}
}
@ActivityScope
public class LocationProviderFactory{
private final GoogleLocationService GoogleLocationService;
private final AndroidLocationService AndroidLocationService;
@Inject
public LocationProviderFactory(GoogleLocationService googleLocationService, AndroidLocationService androidLocationService){
this.googleLocationService = googleLocationService;
this.androidLocationService = androidLocationService;
}
public LocationProvider getLocationProvider(condition){
if(condition){
return googleLocationService;
} else {
return androidLocationService;
}
}
}
А может, есть лучший способ?