The concept of dependency injection (DI) and inversion of control (INVERSION of control) is something that some people find difficult to understand. I recently explained this concept to others and I think non-technical people should be able to understand it, so I share it with you and hope that you can explain it to others next time.

Dependency injection and inversion of control say the same thing, just from different perspectives.

Let’s take superman and the little monster as an analogy.

The earth is threatened, there are constantly small monsters to want to destroy the earth, every little monster we need to find a superman to deal with him, a superman is certainly not enough, because every time to small monsters are not the same, they have the ability is not the same.

So we have to find a suitable superman to deal with him, in the worst case is to every one we’re going to find a little monsters or create a new superman, so to ten little monster, we will be made ten, to will make hundred hundred, thousand, come over, we’re going to make corresponding superman to hundred million, and most of the superman can only be used once.

To solve this problem, we introduced the concept of dependency injection and inversion of control. We separated superman and superpowers, independent superman and independent superpowers. When a little monster comes, we find Superman and give him corresponding superpowers, and let him destroy the little monster.

In this way, we only need a few supermen, and instead of making supermen, we will study how to make more and better superpowers for them to use.

Superpowers and superhumans are no longer strongly dependent. Superpowers are given to Superman from outside. Superman and superpowers are dependent, but this dependency is externally given, so we can say that superpowers are injected into him from outside, so this is called dependency injection.

On the other hand, what kind of super power superman has is not controlled by himself, but by the outside, which is equivalent to handing over the efficacy of super power to the outside, and the outside decides what kind of super power Superman should have. Therefore, the control of super power is reversed from his own control to external control, which is called inversion of control.

This is my easy to understand explanation of dependency injection and inversion of control. It can better solve the problem of strong coupling between objects, but also can do on-demand use on-demand loading.

By the way, I think the reason why Iron Man and Batman are more popular is because their superpowers are given to them, not their own, so they can constantly have new powers to use, which makes them more interesting and fresh.