Dependency Injection (DI) - Cleaner Code ?
Tags: programming, codenewbie, java, oop
Published: 2022-01-25 00:43:05

[What is DI?](#what-is-di-?)

[An example...](#an-example-...)

[Types of DI](#types-of-di)
  - [Constructor Injection](#constructor-injection)
  - [Property Injection](#property-injection)
  - [Method Injection](#method-injection)

[Conclusion](#conclusion)

---

In your life as a Software Developer, no matter if beginner or advanced, you need 
to learn new things to grow. This tutorial could even make you a better developer 
in terms of writing much cleaner code. Interested ? Okay, so let´s move on!

## What is DI ?

What the heck is Dependency Injection ? Do not worry, it´s not that complicated and you can use it in your projects, just right now. Especially in OOP (Object Oriented Programming).

Dependency Injection or DI is a software design pattern, which provides IoC (Inversion of Control) in Objects. 


## An example ...

Let´s assume, you did not completly understand, what Dependency Injection does, okay? 


![First example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93y8jx9lk5mk3796ap2t.png)

We assume that the headchef commands another chef for e.g to cook, but this chef can only use a pan to cook something, but you can use other tools in the kitchen to create something tasty.

![Second Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9v2i8b0efjfljbv2fsdp.png)
However if we let the headchef command the chef to cook, but in this case he gives the chef a pan to cook, then it would not be problem, the chef just need the ability to cook with a tool that is predestined to create something eatible and tasty.

This small example should give you an simple insight into dependency injection.

## Types of DI

In this part I am going to show some simple code snippets of the types of Dependecy Injection. (Java)

Those snippets are simple examples without any further explanation.

### Constructor Injection

```Java
interface Tool{
    void cook();
}

public class Pan implements Tool{
    
    public void cook(){
        System.out.println("Cooking something great");
    }
}

public class Chef {

    private Tool tool;

    public Chef(Tool tool){
        this.tool = tool;
    }

}

public class Headchef {

    private Tool pan = new Pan();
    Chef chef = new Chef(pan);
    
    chef.cook();
}

```


### Property Injection

```Java
interface Tool{
    void cook();
}

public class Pan implements Tool{
    
    public void cook(){
        System.out.println("Cooking something great");
    }
}

public class Chef {

    private Tool tool;

    public Chef(Tool tool){
        this.tool = tool;
    }

   
}

public class Headchef {

    private Tool tool;

    public tool setTool(Tool tool){
        tool = tool;
        return tool;
    }

    public getTool(){
        return tool;
    }


    chef.cook();
}
```


### Method Injection

```Java
interface Tool{
    void cook();
}

public class Pan implements Tool{
    
    public void cook(){
        System.out.println("Cooking something great");
    }
}

public class Chef {

    private Tool tool;

    public Chef(Tool tool){
        this.tool = tool;
    }

   
}

public class Headchef {

    private Tool tool;

    public void giveOrders(Chef chef){
        chef.cook();
    }
}
```

---

## Conclusion

There are pros and cons in this software design pattern in object oriented programming. Although it is been said, that it could make the code cleaner, but also not. The reason sits mostly in front of the computer, but never the less the complexity and the size of the programm matters too. So it is not a complete solution for cleaner code, although it could be (it depends on the developer). But it is definitely a good pattern to test and update the code with less complexity and problems.

Feel free to ask questions :D