Skip to content

Instantly share code, notes, and snippets.

@aveuiller
Last active January 12, 2021 21:59
Show Gist options
  • Save aveuiller/e07577e857a63037dec68506e0c44e27 to your computer and use it in GitHub Desktop.
Save aveuiller/e07577e857a63037dec68506e0c44e27 to your computer and use it in GitHub Desktop.
medium_dependency_injection
public static void main(String[] args) {
// Creating the injection module configured above.
Injector injector = Guice.createInjector(new WeatherModule());
// We ask for the injection of a WeatherContract,
// which will create an instance of ThermometerContract
// with the named TemperatureUnit under the hood.
WeatherContract weather = injector.getInstance(WeatherContract.class);
}
public static void main(String[] args) {
// Not using dependency injection
WeatherContract weather = new WeatherService(true, true, null);
}
public static void main(String[] args) {
// Using dependency injection
TemperatureUnit celsius = TemperatureUnit.CELSIUS;
ThermometerContract thermometer = new Thermometer(celsius);
WeatherContract weather = new WeatherService(thermometer);
}
public class WeatherService implements WeatherContract {
private final ThermometerContract thermometer;
@Inject
public WeatherService(ThermometerContract thermometer) {
this.thermometer = thermometer;
}
}
public class Thermometer implements ThermometerContract {
private final TemperatureUnit unit;
@Inject
public Thermometer(@Named(WeatherModule.TEMPERATURE_UNIT)
TemperatureUnit unit) {
this.unit = unit;
}
}
public class Thermometer {
private final TemperatureUnit unit;
public Thermometer() {
this.unit = TemperatureUnit.CELSIUS;
}
}
public class WeatherService implements WeatherContract {
private final Thermometer thermometer;
// This constructor is not using dependency injection
public WeatherService() {
this.thermometer = new Thermometer();
}
}
public void testTemperatureStatus() {
ThermometerContract thermometer = Mockito.mock(ThermometerContract.class);
Mockito.doReturn(TemperatureUnit.CELSIUS).when(thermometer).getUnit();
WeatherContract weather = new WeatherService(thermometer);
Mockito.doReturn(-50f).when(thermometer).getTemperature();
assertEquals(
TemperatureStatus.COLD,
weather.getTemperatureStatus()
);
Mockito.doReturn(10f).when(thermometer).getTemperature();
assertEquals(
TemperatureStatus.MODERATE,
weather.getTemperatureStatus()
);
}
public Thermometer(boolean useCelsius) {
if (useCelsius) {
this.unit = TemperatureUnit.CELSIUS;
} else {
this.unit = TemperatureUnit.FAHRENHEIT;
}
}
public class WeatherModule extends AbstractModule {
public static final String TEMPERATURE_UNIT = "temp_unit";
@Override
protected void configure() {
// Named input configuration bindings
bind(TemperatureUnit.class)
.annotatedWith(Names.named(TEMPERATURE_UNIT))
.toInstance(TemperatureUnit.CELSIUS);
// Interface - Implementation bindings
bind(ThermometerContract.class).to(Thermometer.class);
bind(WeatherContract.class).to(WeatherService.class);
}
}
public WeatherService(boolean useRealDevice,
boolean useCelsius,
String apiKey) {
if (useRealDevice) {
this.thermometer = new Thermometer(useCelsius);
} else {
this.thermometer = new ThermometerWebService(useCelsius, apiKey);
}
}
public class WeatherService implements WeatherContract {
// We now use the Interface
private final ThermometerContract thermometer;
// New constructor using dependency injection
public WeatherService(ThermometerContract thermometer) {
this.thermometer = thermometer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment