Now that we have an idea of what dependencies are, let's look at dependency injection.
What is it?
Well, dependency injection or Di, is a design pattern used to achieve inversion of control called
IOC between classes and their dependencies.
Instead of a class creating its own dependencies, they are provided from the outside.
So a simpler way to say that would be using your builder that we built in the last video, instead of
the builder going out to buy tools, someone brings the tools to the builder.
This way, the builder can focus on building without worrying about how to get the tools.
And there are different types of dependency injections.
There is constructor injection, which we're going to look at in this video where dependencies are provided
through a class constructor.
So in the constructor you are passing on the tools such as the saw such as the hammer and so forth.
Then there is setter injection where dependencies are provided through a property or a method, and
then there is interface injection where dependencies are provided through an interface that the class
implements.
All right.
So let's look at our example that we had from the last video.
And understand dependency injection especially with the constructor injection.
In our example we used no dependency injection.
So we did have a dependency but we didn't inject the dependency.
How can we change our builder class so that it uses dependency injection?
We can do that by saying that, for example, you need to provide me a hammer whenever you want to use
me as a builder because I am just going to my job, like just doing my job, going to the site, to
the construction site.
But you need to provide me the tools because otherwise I can't do my job.
Okay?
So you need to give me the hammer and you need to give me my saw and whatever other tools we have.
So now, instead of the builder being responsible to create its dependencies, now it happens through
injection.
So we are injecting the hammer into the builder by just saying, okay, I'm going to provide you with
the hammer whenever I'm going to use you, and I'm going to provide you with a saw whenever I'm going
to use you.
Okay.
So this here now is constructor injection or die.
Dependency injection die okay.
Really cool stuff.
So now if I want to use my builder, if I want him to build my house and I want him to use his methods,
basically I want to use his methods to finalize my house.
Um, then I need to provide him with the tools like hammer and the saw.
And so forth.
So now we can go ahead and see a hammer.
Hammer, new hammer, and we can say saw, saw, new saw, and we can say builder, builder, new builder.
And we're passing on the hammer and the saw to the builder.
And now we can just use the builder to do it's methods such as build a house.
So let's run this example to see what's going to happen.
And we will see that we see nothing because we don't have the read line or read key here.
So let's run this again.
And we say we see here hammering nails, sawing wood house build.
Beautiful.
If it always was that simple I think sawing wood should go before hammering nails, but whatever.
Okay, so in this case we have seen how dependency injection works with the constructor.
So I hope you enjoyed this video.
Now you know what dependency injection is, at least on a super simplistic example here.