Java EE - 8 years
Android - 2 years
Did some Java desktop, Javascript, PHP, Databases, iOS along the way
Currently Development Manager of Spredfast Mobile (iOS and Android)
Pet projects (github.com/denisk20)
Dependency injection means giving an object its instance variables. Really. That's it.
James Shore, 22 March 2006
class WeatherActivity {
WeatherManager weatherManager;
@Override
protected void onCreate() {
weatherManager = new WeatherManager();
WeatherNetClient netClient = new WeatherNetClient();
netClient.setRequestQueue(new RequestQueue());
weatherManager.setNetClient(netClient);
}
public void refreshWeather() {
weatherManager.fetchWeather();
}
}
public class WeatherActivity {
@Inject WeatherManager weatherManager;
protected onCreate() {
ObjectGraph.create(new WeatherActivityModule())
.inject(this);
}
public void refreshWeather() {
weatherManager.fetchWeather();
}
}@Module
class WeatherActivityModule {
@Provides RequestQueue provideRequestQueue() {
return new RequestQueue();
}
@Provides WeatherNetClient provideWeatherNetClient(RequestQueue queue) {
return new WeatherNetClient(queue);
}
@Provides WeatherManager provideWeatherManager(WeatherNetClient weatherNetClient) {
return new WeatherManager(weatherNetClient);
}
}Avoid dependency injection frameworks
Is good when right tools are used
by Google
@Component(modules = DripCoffeeModule.class)
@Singleton
interface CoffeeShop {
CoffeeMaker maker();
}
Dagger http://square.github.io/dagger/
(intro talk by Jesse Wilson, Square
http://www.infoq.com/presentations/Dagger)
Dagger 2 http://google.github.io/dagger/
(intro talk by Gregory Kick, Google
Events are great because:
class WeatherActivity {
WeatherManager weatherManager;
/**
* Updates the weather for the first city from the list
*/
void updateWeather() {
weatherManager.fetchCities(new CitiesCallback() {
@Override
public void citiesUpdated(List<City> cities) {
weatherManager.updateWeatherForCity(cities.get(0), new WeatherCallback() {
@Override
public void weatherUpdated(Weather weather) {
//do something with the weather here
}
});
}
});
}class WeatherActivity {
WeatherManager weatherManager;
public void updateCities() {
weatherManager.fetchCities();
}
/**
* Is called whenever cities list is updated by ANYONE
*/
@Subscribe
public void citiesUpdated(CitiesUpdatedEvent event) {
weatherManager.updateWeatherForCity(event.getCities().get(0));
}
/**
* Is called whenever ANY city is updated
*/
@Subscribe
public void cityUpdated(CityUpdatedEvent event) {
City city = event.getCity();
Weather weather = event.getWeather();
//do something
}
}class WeatherActivity {
WeatherManager weatherManager;
TextView wind;
void updateWeather() {
weatherManager.updateWeather(new WeatherUpdatedCallback() {
@Override
public void weatherUpdated(Weather weather) {
wind.setText(weather.getWind());
}
});
}
}class WeatherActivity {
WeatherManager weatherManager; //should be application-scoped
TextView wind;
void updateWeather() {
weatherManager.updateWeather();
}
@Subscribe
void weatherUpdated(WeatherEvent event) {
wind.setText(event.getWeather().getWind());
}
}
by Square
Bus bus = new Bus();
bus.register(this);
bus.post(new MyEvent(myData));
@Subscribe
public void onMyEvent(MyEvent event) {
//do something with event
}
bus.unregister(this);Otto
GreenRobot EventBus
(claims to be like 2 times as fast as Otto)
Testing + Dependency Injection =
by Google
Alternatives:
Questions?