Last active
January 12, 2021 21:59
-
-
Save aveuiller/e07577e857a63037dec68506e0c44e27 to your computer and use it in GitHub Desktop.
medium_dependency_injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
// Not using dependency injection | |
WeatherContract weather = new WeatherService(true, true, null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
// Using dependency injection | |
TemperatureUnit celsius = TemperatureUnit.CELSIUS; | |
ThermometerContract thermometer = new Thermometer(celsius); | |
WeatherContract weather = new WeatherService(thermometer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Thermometer(boolean useCelsius) { | |
if (useCelsius) { | |
this.unit = TemperatureUnit.CELSIUS; | |
} else { | |
this.unit = TemperatureUnit.FAHRENHEIT; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public WeatherService(boolean useRealDevice, | |
boolean useCelsius, | |
String apiKey) { | |
if (useRealDevice) { | |
this.thermometer = new Thermometer(useCelsius); | |
} else { | |
this.thermometer = new ThermometerWebService(useCelsius, apiKey); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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