IOC DI what the hell is that

Hi guys, today I’d like to talk to you about inversion of control and dependency injection.

In the process of understanding, I have seen a lot of information. If there is any incorrect information, I hope you can contact me and learn together

This article is divided into the following three parts

  • What are IOC and DI
  • Benefits of IOC and DI
  • How to use IOC and DI in Java

What are IOC and DI

Inversion of Control, or IoC, is a design principle used in object-oriented programming to reduce coupling between computer code. One of the most common is called Dependency Injection, or DI, and another is called Dependency Lookup. With inversion of control, when an object is created, it is passed (injected) a reference to the object on which it depends by an external entity that regulates all objects in the system.

What exactly does inversion of control reverse, what does it control, what does dependency injection inject, what does it depend on?

First of all, let’s forget about inversion of control and look at dependency injection, because back in 2004 Uncle Martin came up with a more understandable term for inversion of control called dependency injection, and understanding dependency injection is a good way to understand INVERSION of control. Here is an example to introduce dependency injection.

Each text input box has a text checker, which is required each time a text selector box is initialized.

public class TextInput {
    private  SpellCheck spellCheck;
    public TextInput(a) {
        spellCheck=newSpellCheck(); }}Copy the code
public class TextInput {
    private  SpellCheck spellCheck;
    public TextInput(SpellCheck spellCheck) {
        this.spellCheck=spellCheck; }}Copy the code

Looking at the two different pieces of code above, we can see that the essential differences are when and where the SpellCheck object is created. In the following code, the text checker is passed inside the text box as a parameter in the constructor of a text box. You can inject a spellCheck object into the TextInput class. The textbox dependency checker, which puts the dependent class as a parameter in the dependent class, is called dependency injection.

The next step is inversion of control. Those of you who have written about the Java Web have used the @AutoWired annotation, which allows you to generate a class object directly without explicitly creating a new one. Control is when we can control when an object is created, and taking that away through the IOC container is a power reversal.

If you understand the concepts above, you know that inversion of control is a dependency injection Web application that uses a container to manage the classes to be injected. It makes it easy for users to repeatedly create dependent classes outside of the class. So an in-depth understanding of dependency injection is key, and inversion of control is the application of DI. As Martin puts it, “What aspects of control have been reversed?” That’s the question. He concluded that the acquisition of dependent objects was reversed,

Let’s talk about the benefits of dependency injection and how it can be done.

Let’s go through the code to understand its benefits

public class Car {
    private enginer enginer;
    private wheel wheel;

    public Car(a) {
        enginer = new enginer();
        wheel = newwheel(); }}Copy the code
public class Car {
    private enginer enginer;
    private wheel wheel;

    public Car(enginer enginer, wheel wheel) {
        this.enginer = enginer;
        this.wheel = wheel; }}Copy the code

Automobile is a complex object, including many of the components, the engine, wheels, etc., all kinds of car is always the same, the size of the wheel, when we want to create two different car, but they only have the size of the wheel not at the same time plan 1 to create a new car class in the constructor’s new new object of a wheel, The second option is to simply create the wheel object outside and pass in the car class. Reduced the number of new Car classes. That is, there is less coupling between classes, and objects are given their dependencies at run time rather than at compile time (car build time). So we can now change the wheels anytime we want. Here, the dependency (wheels) can be injected into the car during running time. This is also a very useful testing technique because it allows dependencies to be simulated or removed.

What are the other ways to implement dependency injection other than through constructors?

Dependency injection is implemented in the following ways:

  • Interface-based. Implements specific interfaces for external containers to inject objects of dependent types.
  • Based on set method. Implement a public set method for a specific property to let the external container call in an object of the dependent type.
  • Based on constructors. Implements a constructor for a particular parameter that passes in an object of the dependent type when creating a new object.
  • Based on annotations. Java-based annotations, such as “@autoWired” in front of a private variable, allow external containers to pass in the corresponding object without explicitly defining the above three types of code. This scheme is equivalent to defining a public set method, but because there is no real set method, it does not expose interfaces that should not be exposed for dependency injection (because the set method only wants to be accessed by the container for injection and does not want other dependent objects to access it).

Ok. So much for IOC and DI. If the above error please contact me and put forward, thank you very much.

reference

Effective Java third edition

wiki

what is dependency injection

What is Inversion of Control?

DI proposed the original text

Spring IOC DI understands