Dagger 2 for beginner Android developers. Dependency Injection Part 2

Published on December 01, 2017

Dagger 2 for beginner Android developers. Dependency Injection Part 2

Original author: Hari Vignesh Jayapalan
  • Transfer
This article is the third part of a series of articles intended, according to the author, for those who can not figure out the implementation of dependencies and the Dagger 2 framework , or are just going to do it. The original was written on December 1, 2017. Free translation.

image

This is the third article in the Dagger 2 series for beginner Android developers . If you have not read the previous ones, then here you are .

Series of articles



Earlier in the series of articles


In the last article, we looked in more detail at dependencies and dependency injection. We also examined an example - the battle of the bastards, where we created a class Warwith strong connections (hard dependencies) .

Using Dragon Glass


Let us recall the example considered earlier - the battle of the bastards. The class Warhas two strong connections: Boltonsand Starks.

public class War {
    private Starks starks;
    private Boltons boltons;
    public War(){
        starks = new Starks();
        boltons = new Boltons();
        starks.prepareForWar();
        starks.reportForWar();
        boltons.prepareForWar();
        boltons.reportForWar();
    }
}

It's time to take advantage of the dragon glass and destroy the White Walkers. Yes, we will use dependency injection to eliminate strong ties.

Recall the idea of ​​dependency injection. A class should not create other classes. Instead, it should get dependencies outside. Let's get the dependencies Boltonsboth Starksexternally through the class constructor War.

public class War {
    private Starks starks;
    private Boltons boltons;
    // Внедрение зависимостей(DI) - получение зависимостей из другого места через конструктор
    public War(Starks starks, Boltons boltons){
        this.starks = starks;
        this.boltons = boltons;
    }
    public void prepare(){
        starks.prepareForWar();
        boltons.prepareForWar();
    }
    public void report(){
        starks.reportForWar();
        boltons.reportForWar();
    }
}

The example above is a variant of dependency injection through the constructor. You probably now think that you are constantly doing this in your projects. True, many use the concept of dependency injection without being aware of it.

ClassWarmust know not only how to perform a specific task, but also where to look for classes that he needs to complete his tasks. If we provide everything necessary for work to our class outside, we will get rid of the previously considered problems. A class can easily work with any instances of other classes that it needs to perform tasks, and will simply be tested in isolation from them. In an application that uses dependency injection, objects will never look for dependencies or create them within themselves. All dependencies are provided to him or embedded in him ready to use.

At some point, of course, someone should instantiate dependency classes and provide them to objects that need it. Typically, this work is done at the entry point to the application. In a regular Java application, for example, such code can be found inside the method main(), as shown below. On Android, this is usually done in a method onCreate()inside an Activity .

public class BattleOfBastards {
    public static void main(String[] args){
        Starks starks = new Starks();
        Boltons boltons = new Boltons();
        War war = new War(starks,boltons);
        war.prepare();
        war.report();
    }
}

In the class, BattleOfBastardswe create dependencies Boltonsand Starksinject them from the constructor into the class War. The dependent class Wardepends on the dependencies Boltonsand Starks.

Time to appreciate yourself and celebrate. Yes, we destroyed the White Walkers (strong ties)! I hope you understand the concept of what we are trying to make out.

Summary


In this article, we realized that a class should not create dependencies within itself. Instead, he should receive them outside.

We also saw a plan to easily implement dependencies in action. We took the example of the battle of the bastards and tried to eliminate strong ties through dependency injection.

media
image

What's next?


In the next article, we will discuss the Dagger 2 library. We will look at the annotations and how Dagger 2 will make it easier for us to implement dependencies when we have to deal with more complex and complicated projects. The next article will be released in a week.