📜 ⬆️ ⬇️

Dagger 2 for novice Android developers - Introduction

This article is the first part of a series of articles, which, according to the author, are intended for those who cannot understand the implementation of dependencies and the Dagger 2 framework, or are only going to do it. The original was written on November 18, 2017. Images and GIF - from the original. Free translation.

image


Dagger 2 is a completely static framework for injecting dependencies in Java and Android , running at compile time. Dagger 2 is an adaptation of the Dagger framework, created earlier by Square , and supported by Google .
')

Who is this article for?


If you are a beginner Android developer , learning Android directly and acquiring Java knowledge in parallel, then this article is for you. If you tried to study Dagger 2 and what you found on the Internet seemed a bit difficult to you - do not worry, I also went through this (we are all a bit special, everyone needs their own approach in explaining something) and this article is definitely for you. If you are already familiar with dependency injection and Dagger , then you can learn something new or clarify some things for yourself.

Article series



Requirements


It is assumed that you are already familiar with the Java programming language , OOP principles and Android development.

What is addiction?


Note: to explain this concept, I will use analogies with the television series "Game of Thrones." If you are not familiar with this series, you can replace the class names with more convenient ones. And you should definitely start watching this show.

Understanding dependencies and relationships is the first step for a clearer understanding of the concepts of object-oriented programming. So what is addiction? For example, we have the Targaryens class, which uses another class or interface, called Dragons . So the Targaryens class depends on the class or interface of the Dragons .

Gif


This means that the Targaryens class cannot work without Dragons . It also means that wherever the Targaryens class is used, the Dragons class will be present, that is, we cannot reuse Targaryens without reusing Dragons . In this case, Targaryens is a dependent class, and Dragons is a dependency. The addict depends on its dependencies.

Two classes that use each other are called 'coupled' . Relationship between classes can be either strong ( 'tight' ) or weak ( 'loose' ). Dependencies are always directed, that is, the Targaryens class depends on the Dragons , but the Dragons class may not depend on the Targaryens class.

 class Targaryens { public Targaryens() { // ,    Targaryens,     Dragons Dragons dragons = new Dragons(); dragons.callForWar(); } } 

What are bad addictions?


A large number of dependencies in a class leads to hard linking problems, which is bad for the following reasons:


# Reuse (reusablility)


When classes and methods are loosely coupled, unrelated, or independent of many others, the reusability of the code increases. Code reuse is one of the basic ideas of object-oriented programming.

#Testing


For testing, you can mock specific objects. But if a class or method has a lot of dependencies, then it will be difficult to test it. If a Java class (for example, Targaryens ) creates an instance of another class ( Dragons ) through the new operator, then this class ( Targaryens ) cannot be tested independently of the class that was created through the new operator ( Dragons ).

# Maintainability


When the code cannot be correctly tested, parts of it are impossible or difficult to reuse, and the project continues to grow, then the project becomes very difficult to maintain. Supportability depends on many other factors, but as long as new developers in your team understand the system and make their colleagues' work more comfortable, your project is still supported.

Types of dependencies


There are many types of dependencies, but you can select the main ones:


# Class dependency


We looked at an example of class dependency earlier. The Targaryens class Targaryens depends on or needs the Dragons class to call the callForWar() method.

# Interface dependency


The executePlan method accepts the WarStrategy interface as a dependency. WarStrategy can be implemented by all houses (Targaryens, Starks, Lannisters, and so on).

 public Result executePlan(WarStrategy strategy) { //WarStrategy    } 

# Dependence on methods or fields


The following method takes a HouseClass object, and the getKing() method it getKing() is a dependency that cannot be identified in the method signature.

 public String extractName(HouseClass house) { return house.getKing(); //     } 

# Direct and indirect dependencies


Let me take a look at another example. The Targaryens class depends on the Starks class in conquering the throne. But Starks depend on other kingdoms of the north, for example, Mormont . So, it turns out that the Targaryens directly dependent on Starks and indirectly on Mormont .

Summary


If the Java class creates an instance of another class through the operator new , then this class cannot be used or tested independently of the class that was created through the operator new . This is called addiction.

Dependencies are bad because they reduce the reusability of the code, and also complicate the testing process, which makes it harder to maintain the project.

Dependencies can be of different types: class, interface, methods, fields, direct, indirect, and others.

What's next?


In the next article we will talk about solving hard linking problems, and yes, you are right, through dependency injection. Consider a real-life example, find a strong connection and analyze it.

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


All Articles