Part 1: Simple Ways To Stab With Dagger 2 ( Scenario )
The blog posts in this series is solely focused on
- Understanding the basics of Dagger 2
and not focused on
- Dependency Inversion principle
- Why to use Dagger 2 || What is Dagger 2 ?
- Architecting Android application with Dagger 2
For using Dagger 2 some of the following links are essential
Following topics are covered in this series
- Module , component and field injection
- Method injection and constructor injection
- Module dependencies and named providers
- Component dependency and subcomponent
- Scopes and Singleton
- Life of a scoped object
- Custom Scopes
Scenario
Let us first discuss the scenario in which we are going to use Dagger 2


We have two activities
- Hotel A
- Hotel B
Hotel A has two fragments
- Restaurant A
- Restaurant B
Let us consider that Restaurant A, B and Hotel B all serves coffee but with different flavors. To brew the coffee all the 3 classes require CoffeBrewer class
public class CoffeeBrewer {
private Water water;
private Coffee coffee;
private WaterHeater waterHeater;
public CoffeeBrewer(Water water, Coffee coffee) {
this.water = water;
this.coffee = coffee;
waterHeater = new WaterHeater(this.water);
}
public void brewCoffee() {
waterHeater.on();
waterHeater.off();
Timber.d("Brewing %s coffee with %s flavor", water.isWaterHot() ? "Hot" : "Cold",
coffee.getFlavor());
Timber.d("----------- Coffee is ready to be served ---------------------");
}
}CoffeeBrewer is a simple class which takes Water and Coffee as constructor parameter and then brews coffee. ( Note: Let us not focus on all the internals of CoffeeBrewer. Let us imagine CoffeeBrewer is simply a machine which requires water and coffee to brew a coffee). So, if all the restaurants and hotel require CoffeeBrewer, they need to construct CoffeeBrewer object in the following manner
private void brewUsual() {
Water water = new Water(waterQuantity);
Coffee coffee = new Coffee(flavor);
CoffeeBrewer coffeeBrewer = new CoffeeBrewer(water, coffee);
coffeeBrewer.brewCoffee();
}If anything changes in constructor of CoffeeBrewer, all the 3 classes have to make necessary changes. To avoid changing in all the 3 classes, normally we create an utility class which does all the object creation of CoffeeBrewer for us. To keep it simple, we create an helper who handles all the coffee brewing part for all restaurants and hotel. Let us name it as a CoffeeHelper
public class CoffeeHelper {
public CoffeeHelper() {
}
public CoffeeBrewer getCoffeeBrewer(int waterQuantity, Coffee.Flavor flavor) {
Water water = new Water(waterQuantity);
Coffee coffee = new Coffee(flavor);
return new CoffeeBrewer(water, coffee);
}
}and use in our restaurants and hotel as following
private void brewWithHelper() {
CoffeeHelper coffeeHelper = new CoffeeHelper();
CoffeeBrewer coffeeBrewer = coffeeHelper.getCoffeeBrewer(waterQuantity, flavor);
coffeeBrewer.brewCoffee();
}So, if anything changes we need to modify mainly in CoffeeHelper class and all the 3 classes can perform their actions without much hassle.
Now, let us see how Dagger helps in creating CoffeeHelper for us i.e. we simply create a variable and Dagger will provide us with the CoffeeHelper instance in the next series post.