Does it have something to do with doctors? Is it a medicine? Why do I have to use it? Can I skip this horror?

Well, once upon a time, a wise man named James Shore, the author of “The Art of Agile Development,” said:

“Dependency injection is a 25-dollar term for a 5-cent concept.”


The concept is actually really simple: Giving a component all the things it needs to do its job. What?! Let’s see:

We have two classes – MainClass and AClass. In MainClass, we need to work with AClass. Instead of directly creating a reference to AClass, we pass it as a parameter to MainClass through its constructor (constructor injection).

  • This is done using abstraction or protocols (similar to interfaces in C++ and C#).
    We define a protocol, and AClass conforms to it by implementing it.

In the MainClass constructor, we receive an instance of the protocol’s type, and since AClass conforms to the protocol, we pass AClass() as the parameter.

It may sound complex, but it’s a clever technique to make code more flexible and maintainable…

Leave A Comment

Receive the latest news in your email
Table of content
Related articles