Explain inversion of control (IoC) and dependency injection (DI) in natural language

purpose

Inversion of Control (IoC) and Dependency Injection (DI) are ideas in software design, not specific technologies. So since it's just a thought, it can still be clear without a programming language.

So, this article will use everyday natural language to explain IoC/DI clearly.

example

Specific examples are the best way to explain ideas, so we will first use specific examples to describe the following.

first step

Now that the New Year is approaching, many companies will organize annual meetings, so the boss of XX company let Xiao Ming be responsible for organizing [XX company's 2018 New Year annual meeting]. This is the overall background of our example.

Xiao Ming had no choice but to agree, but then reflected to the boss: "I can't do it all by myself, you have to find someone to help me."

The boss felt reasonable, so he thought about it and said, "Okay, then I'll go to Xiaogang and talk about it. You can find him tomorrow."


At this point, the logic basically took shape: the boss asked Xiao Ming to organize the annual meeting and told him to let Xiao Gang assist him.
Then we came to the conclusion that Xiao Ming was dependent on Xiao Gang at this time, because the boss directly told him to let Xiao Gang assist him.

If everything is normal, of course there is no problem, but if there are variables, then the problem is coming!

problem

The boss found Xiao Gang and told him to assist Xiao Ming in organizing the annual meeting, but Xiao Gang said that he had a very urgent matter on his hands and could not spare time recently. /(ㄒoㄒ)/~~

The boss saw that it was indeed the case. There was no other way but to find someone else to replace it; and the person who needed to tell Xiaoming to help him had changed, just because the boss just told Xiaoming directly to let Xiaogang come to help him.

Second step

Who is going to help Xiao Ming organize the annual meeting? The boss now has a few candidates in his mind, but he is not sure whether it can be.

With the previous lesson, the boss did not directly say the name of the person, but said to Xiaoming: "I will arrange another person to assist you, and he will come to you tomorrow."


What is the difference between now and the first step? It was the boss who did not specify who should assist Xiao Ming, but gave a general concept-there is a person, and the attribute of this person is to "help organize the annual meeting."

In this case, Xiao Ming does not rely on specific people, but only on the people [helping to organize the annual meeting]. If Xiaohong is asked to assist Xiaoming in organizing the party the next day, then Xiaohong is also a person who relies on [assist in organizing the annual meeting].

The above process from the first step "Xiao Ming directly relies on Xiao Gang" to the second step "Xiao Ming relies on the people [assisting the organization of the annual meeting], and Xiao Hong also depends on the people [assisting the organization of the annual meeting]" is what we often say The principle of "dependency inversion".


In addition to the above point, there is another difference, that is: in the first step, the boss asks Xiao Ming to find Xiao Gang, and in the second step, the boss asks someone from [Assisting in Organizing the Annual Meeting] to find Xiao Ming.

Because now Xiao Ming relies only on the people [assisting the organization of the annual meeting], he doesn't know who it is, and the boss arranges it, so it is not scientific to let Xiao Ming take the initiative to find the helper, and the boss just asks [assistance] The person organizing the annual meeting will come to Xiao Ming tomorrow, so Xiao Ming just needs to wait.

This logical idea is called inversion of control, that is to say who originally organized the annual meeting with Xiaoming, the specific control is in the hands of Xiaoming; but now the control is in the hands of the boss, the control is reversed, so The name is "inversion of control", or IoC.

Seeing this, do you think the boss is similar to the emperor in feudal society?^_^

third step

The next day Xiaohong came to Xiaoming and said that she was here to help him organize the annual meeting, so the two started to work. During the organization of the annual meeting, they need to coordinate various resources, such as dining, programs, logistics, etc. When other personnel cooperate, they all find the boss to coordinate, and the boss arranges.


It seems to be progressing well at the moment. Xiao Ming doesn’t care about which specific things are done by who, but rely on the virtual [person who arranges dinner], [person who performs the show], [logistics support person], and who is the specific one. It's up to the boss to arrange it.

But this will also cause problems.

problem

All personnel arrangements are decided by the boss, and as things get more and more, the boss can't handle it anymore.


This problem is also very realistic. A complete annual meeting may require dozens of people to collaborate. Everyone finds their boss when they have something to do. It must be too busy.

But what can be done? As we mentioned above, because it is now in the "inversion of control" state, all personnel arrangements are in the hands of the boss, and other people will not find specific people by themselves.

the fourth step

Finally, the boss decided to use the big move-compiled a "annual meeting organization staff list."
1. It lists common types of work, such as venue arrangement, venue layout, venue cleaning, party director, party photography, etc.;
2. Then let the person in charge of the specific work fill in his own name, such as Zhang San responsible For the layout of the venue, Li Si is in charge of the party director, and Wang Wu is in charge of the party photography;
3. If there are changes in personnel, go to the boss to modify the list; if there are other new tasks, add an item to the list, and then change the details Fill in the name of the personnel;
4. When other personnel need someone to help, they only need to find the specific executive personnel in this list.


In this step, all the staff still meet the principle of "reliance inversion". For example, Zhang San relies on [venue layout], Li Si relies on [evening party director], and Wang Wu relies on [evening party photography]; If Li Si needs someone to help with photography, then Li Si also relies on [Party Photography].
Moreover, in this step, it is still in a state of "inversion of control". For example, when Li Si is now directing the party, he needs a photographer to record the video. At this time, he cannot control who is going to record the video. It is still controlled by the ("Annual Meeting Organization" The staff list" and the boss) are controlled by the whole.

If neither "dependency inversion" or "inversion of control" has changed, then what has changed?

In fact, the difference is that in the third step, the boss directly arranges specific people to do specific things, so he needs to remember who does what, which means that the boss directly relies on Zhang San, Li Si, and Wang Wu. Specific people; and in the fourth step, the boss does not need to memorize specific names, but asks them to sign up on the list, so the boss does not rely on specific Zhang San, Li Si, and Wang Wu.

This means that we inject all the dependencies of [Venue Layout], [Party Director], and [Party Photography] and their specific executors into the "Annual Meeting Organization Staff List", and other people only need to query the list. The boss just provided such a rule, and he and the specific personnel arrangements were relieved.

The above process is "dependency injection".

to sum up

The above example is a step-by-step description of dependency inversion-inversion of control-dependency injection. The key is the split and transfer of dependencies.
Before the inversion of dependence, only Xiaoming relied on Xiaogang. After the inversion of dependence, Xiaoming relied on [Assist Organization Annual Meeting] and Xiaohong also relied on [Assist Organization Annual Meeting].
Before the reversal of control, Xiao Ming went directly to Xiaogang. After the reversal of control, Xiao Ming relied on the people [assist in organizing the annual meeting]. This dependency was handed over to the boss to complete.

The former boss of the dependency injection held control, and he relied on Wang Wu and [evening party photography]. After the dependency injection, the boss just made rules. He did not rely on Wang Wu or [evening party photography].

Guess you like

Origin blog.csdn.net/jhq1990/article/details/79265267