I write it to convince myself because it doesn't look right even if I look at various explanations.
First of all, if it is a textbook style
"DI realizes" Inversion of Control Principle ", which is one of the design patterns [^ 1]". You can separate the instantiation and dependency resolution that make up a component from the source code. (From a thorough introduction to Spring by NTT DATA Corporation)
I don't know what you're talking about. For the time being, let's think from a place that is easy to understand.
[^ 1]: Collection of design know-how in object-oriented languages
For the time being, what to do is aside, what can be improved by DI?
・ Reduced dependency between classes = Loose coupling between classes (becomes loose coupling) ・ ↑ makes it easy to reuse classes, etc. ・ ↑ makes unit tests easier
It seems that. There seems to be a core here because the word "dependency" has come up many times. So what are "dependencies" in programming?
If you use textbook words, "One module changes or depends on the internal behavior of another module = If you change the specifications of a dependent module, the contents of the module are likely to be corrupted."
What is a configuration with high dependency in Java language etc. -A new instance of another class is created in the method of one class. -Using an instance of another class as an argument of a method of one class And so on. If it is a concrete code
Hoge.java
class Hoge{
public void hoge(){
Fuga fuga = new Fuga();
}
}
It has become quite concrete. If you use a new class in one class, and if there is a change in the constructor of the new class, you can imagine that the method of the caller must also be changed. In addition, if you change the method in the class that creates the instance, the processing of the caller will be messed up. The solution to this problem is to make the calling class use the interface instead of creating an instance by newing the implementation class, and make the new class a class that inherits the interface. That is.
Fuga.java
pulic interface Fuga{
int a;
void foo();
}
HogeFuga.java
pulic HogeFuga implements Fuga{
int a;
void foo(){
fugafuga;
};
}
Hoge.java
class Hoge{
public void hoge(){
Fuga fuga = new HogeFuga();
}
}
By using this method, you can freely use the class that implements the interface, and you can reduce the dependency. However, since the interface class cannot be new after all, it is necessary to describe the implemented subclass to be new at the caller.
Therefore, describe the relationship using xml files and annotations. ↓ This is "DI"
Hoge.java
class Hoge{
Fuga fuga;
public void hoge(){
}
public void setFuga(Fuga fuga){
this.fuga = fuga;
}
}
At this time, any class that implements the Fuga class can be passed to the setFuga class, and the Hoge class does not care which class is actually used.
Is this the mainstream when using Spring?
Hoge.java
class Hoge{
@Autowired
protected Fuga fuga
public void hoge(){
}
}
What is DI? = I want to reduce the binding of the class I use!
Recommended Posts