DEV Community

Cover image for Dependency Injection (DI) - Cleaner Code ?
Richard Rhein
Richard Rhein

Posted on

Dependency Injection (DI) - Cleaner Code ?

What is DI?

An example...

Types of DI

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

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
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

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();
}

Enter fullscreen mode Exit fullscreen mode

Property Injection

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();
}
Enter fullscreen mode Exit fullscreen mode

Method Injection

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)