📜 ⬆️ ⬇️

Dagger 2 for novice Android developers. The introduction of dependencies. Part 2

This article is the third part of a series of articles intended, according to the author, for those who cannot deal with dependency injection and the Dagger 2 framework, or are only going to do it. The original was written on December 1, 2017. Free translation.

image

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

Article series



Earlier in the series of articles


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

Dragon glass use


Let us recall the example considered earlier - the battle of bastards. War has two strong connections: Boltons and 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 links.

Recall the idea of ​​introducing dependencies. A class must not create other classes. Instead, it should get dependencies outside. Let's get the Boltons and Starks dependencies outside, through the constructor of the War class.

 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 a constructor. You probably now think that you are constantly doing this in your projects. True, many use the concept of dependency injection, unaware of it.

The War class needs to know not only how to accomplish a specific task, but also where to look for the classes it needs to perform its tasks. If we provide everything necessary for the work to our class outside, then we will get rid of the previously considered problems. The 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 search for dependencies or create them within themselves. All dependencies are provided to it or embedded in it ready to use.

At some point, of course, someone must create instances of dependency classes and provide them to objects that need it. Usually such work is performed at the point of entry into the application. In a regular Java application, for example, such code can be found inside the main() method, as shown below. In Android, this is usually done in the onCreate() method inside the 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 BattleOfBastards class BattleOfBastards we create the Boltons and Starks dependencies and Boltons through the constructor into the War class. The dependent class War depends on the Boltons and Starks dependencies.

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 for simple dependency injection 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. Let's look at annotations and how Dagger 2 will make it easier for us to implement dependencies when we have to deal with more complex and confusing projects. The next article will be released in a week.

Source: https://habr.com/ru/post/343658/


All Articles