Dependency Injection :

I, Arslan Mirbzergi,I intend to talk to you about this issue in this article. Dependency injection is a design pattern that aims to eliminate dependency between two classes by creating different codes or codes. If you take a look at the things you deal with on a daily basis, you’ll see that they come from different parts. Devices such as computers or laptops that use screens, RAM, processors, fans, power, etc. are composed. Each of these parts, if they are damaged for any reason, is usually the first thing you do is to replace these parts, but assuming that these parts cannot be repaired. In this case, as soon as the piece is damaged, you need to replace your device altogether and buy a new one.
In order not to cause this problem, electronics manufacturers usually make parts of any device repairable or replaced, called modular design. The same situation can prevail in the field of programming. This means that the programmer reduces the dependencies in the program from the class level and adds it again when needed. This is called dependency injection.
dependency-injection

Dependence injection in Java

We said that dependency injection is a design pattern that aims to eliminate dependency between two classes by creating different codes or codes. Suppose we want to do a dependency injection in the Java programming language. When Class 1 depends on Class 2, this dependency is done by defining a field of Class 2 in Class 1. After that by the new keyword, we generate the object from Class 2.
public class Class1{
public Class2 class2 = new Class2();
}

Precise definition of dependency injection injection

Dependency injection is a technique in programming. By this technique, each class operates independently of its affiliates. This is done by separating “object construction” from “object use”, so programmers can fully apply two important principles of object-oriented design, which is the principle of reverse dependency or dependency inversion principle and single responsibility principle.
Dependency injection makes us reach a free connotation and our classes have the least dependence on each other.
In the dependency injection process, there are 4 main roles that we will explain about each one below.

Types of sections:

Service Object: Contains any object that provides a service.
Client Object: The client object contains any object that uses the service object.
Interface: Interface is actually client expectations of service. Interface is implemented by the service and after that, the injection process is performed by interfaces. Of course, interfaces cannot be considered as concrete classes because inside the Interface, nothing runs and the Interfaces are at the level of abstraction or abstraction.
Abstract is a general view of an object. For example, when we think about a chair, we only imagine a general shape, but when we mention a particular type of wheelchair, for example, it plays exactly the shape of that particular chair in our minds. Client is usually not allowed to receive any information about the way the Services are used. Client interface and service class is Interface.
Injector: By Injector, the services are introduced to client. The Injector collection is located in programming languages in the form of external packages and there is no need to develop Injector by the programmer.

Why do we use dependency injections?

Before we answer this question, you need to know that some kind of dependency injection called hard dependency injection, this dependency injection creates problems that increase as the program expands. Problems such as:
  • Reduce the ability to expand the program
  • Reduced maintenance ability
  • Reduced reuse ability
  • Reduced testing ability
The main purpose of dependency injection is to create dependencies independently of each other so that the main class or object made can be easily tested or implemented. If the class is not separated from its affiliations at the time of construction, the entire class must be changed if it decides to make any changes in the future.
dependency-injection

Methods of using dependency injection in Java language

Constructor Injection Method

In the constructor method, an object is sent from the lower class to the higher class whose material is interface.

Method injection

We said that in the constructive method, an object is sent from the lower class to the higher class, the interface. That is, every lower class necessarily uses a high class. But if we want to choose only one specific method of class for dependence injection, we need to send this dependence to an argument in a given method. In this case, dependency injection is performed solely on the same method. In other words, dependency is sent from the lower classes to the method in the upper classes.

A more accurate expression of the injection of dependency in Java, in the form of an example

Imagine wanting to create a section on your computer where, to email users after registering a comment. In this case, you will build a class by which this will be done for you. Now suppose that after a while you want to add the ability to send SMS as well. In this case, you will have to create a new class for this purpose. In other words, your codes are constantly changing. You might ask if we can replace this code change? The answer is yes. You will be able to do this by injecting dependency in Java. This means that you can send it to the user in email or SMS format by having a recipient’s address and a pre-written text.
For this purpose, you need to create the main class by injecting dependency in Java as well as having an interface or interface so that you can use this class if you add any possible changes.
dependency-injection
With a practical example, we make it clearer. Suppose you have a class called MessageService. The task of this class is to send messages. This task is performed in 2 ways. The first method is without the use of DI (an acronym for Dependency Injection) and our final code is as follows:
class TypeA{
 sendMessage(){
---------
---------
}
}
 
class TypeB{
 sendMessage(){
---------
---------
}
}
class SendMessageApplication{
main(){
 send(type);
}

send(int type){
if(type == 1){
TypeA a = new TypeA();
 a.sendMessage();
}else{
TypeB b = new TypeB();
b.sendMessage();
}
}
}

In that case, if we want to make a change in Class A, we have to do it in Class C as well. It’s about simple projects, maybe practical, but in the case of complex projects, it’s going to be very hard and time-take. In this case, what is the solution to this problem? To solve this problem, we need to build an interface or interface. This Interface has a method called sendMessage and is responsible for sending messages. In this case, all servers at our disposal (message sending classes) are required to implement this interface. That way we have classes that all use the interface. SendMessage method is responsible for sending messages in English and Persian.
In the next step, by injecting dependency, in the setter function and in the MessageApplication class, the type of service (message classes) that we are considering, and by messageService.sendMessage(); This method refers to the sendMessage() function of our interface, run the program and send the message. Therefore, in the second method, which is used by dependency injection, our final class will be as follows:
interface MessageService{

sendMessage();

}

class TypeA implements MessageService{

void sendMessage(){

//------------

//------------

}

}
class TypeB implements MessageService{

void sendMessage(){

//-----------

//-----------

}

}

//----------------------------------------------------

class MessageApplication{

MessageService messageService;

void setMessageService(MessageService service){

this.messageService = service;

}

void sendMessage(){

messageService.sendMessage();

}

}
After this, you will be able to use 2 methods of sending messages (grades A and B) by creating an object from the MessageApplication class and calling sendMessage(). You can even use other types of classes. Only use the new type without applying any changes to the MessageApplication class.

Advantages of dependence injection

Dependency injection has many advantages, some of which we will mention below:
  1. In dependency injection, you will not need to re-compile the code and the codes will be compile when running.
  2. Reduce the code used in the class
  3. The process of developing codes is done online and they are used in the application.
dependency-injection

Disadvantages of using dependency injection

In addition to the significant benefits of dependency injection, there are disadvantages that include:
1- The process of reading the code becomes very difficult. This is due to the lack of all codes in one section and their dispersion in independent classes.
2- Dependency injection requires a high level of experience in coding and execution. If you are not skilled in it, you will mess up the program.
3- Injection management of dependency is complicated.

And in the end,

Mirbazorgi’s website aims to help you learn and fix your problems by providing articles and practical experiences. Email me if you have any questions.