Okay so we saw constructor dependency injection.
Now let's look at setter dependency injection.
So how is it different.
Well the thing is in order to do that we are going to use properties now instead of what we did before.
So for example we're going to create a hammer that will be the property for the hammer.
And then we're going to use another property for the soul okay.
So and this one will be a soul or it will be called soul.
Actually we should call the properties with a capital letter.
So we have the soul now.
And now instead of the builder using constructor dependency injection, what we're going to use is setter
dependency injection.
So setter D so in setter D we actually don't even need to call the constructor at all.
So we can have a default constructor.
All right.
We don't need to create one ourselves.
So even if you don't add a constructor there is one provided by default as we've seen before.
And now we can use the builder.
But we mustn't pass anything to the builder.
So this new builder will still work.
It will just create a empty builder object which has properties that aren't assigned yet.
Right.
And has this method that could be called, but is not really ready because neither the hammer nor the
saw are set.
They are empty at this point, right?
So that means before we can use the saw and the the hammer, we need to provide them to the builder.
So we're creating a builder.
And before we can call this build house method, if we call it we should get an error.
You see here null reference exception.
So the problem here now is we are not passing the hammer nor the saw to our builder, not during the
constructor but during the setter.
So how we can do that is we can just say builder dot hammer is going to be the hammer that we just created
earlier on in this line.
And now we can do the same thing with builder Dot saw and we can provide it the saw from this line.
And now at this point we can build the house, we can run our code and it will say hammering nails,
sawing wood, build, house build.
So what we see here is setter dependency injection where we are setting the dependencies.
So the dependencies are hammer and saw and we are setting them in these two lines.
Okay.
So what we're doing here is we inject dependencies via setters okay.
This happens here as well as here.
And here we're creating the dependencies outside.
Good.
So the cool thing is they can now be set from outside the class.
And this gives us flexibility.
So dependencies can be changed after the builder object has been created in our earlier example where
we had the constructor, we could not change the type of hammer or the hammer that is used.
So in our particular example that we have here, obviously the saw and hammer, they are fixed.
But imagine we could use polymorphism where we inherit, let's say a rubber hammer that is also a hammer.
So it has the same capabilities as a hammer.
But on top of that a rubber hammer can be used for other things, such as laying down the I don't even
know the English word, but like another hammer has other capabilities.
And on top of the default hammer, or like an electric saw is more powerful than a human saw, let's
say.
So we could have a saw that inherits from saw, but then is better because it has additional skills.
And then we could still say, okay, we start giving the builder the default saw and the default hammer,
but actually we're going to override them later on.
So you could obviously combine the constructor die with the setter die.
So with the setter dependency injection.
Good.
So that's that for this one.
And in the next video we're going to look at interface dependency injection.
So see you there.