Skip to content
Vikyath Shirva

Dependency Injection at a high level

- Vikyath Shirva

If you are hearing this term for the first time in object oriented design, it might put you in a haze. What is it and why do we need it?

The main intention or purpose of this concept, is to decouple the dependency relationships between objects. Now in any object oriented design, we have a lot of objects that are pretty important to the functioning of the application. There can exists some meaningful relationships between these objects for the purposes of how you design it or how the business logic should work. We establish these relationships with dependencies. Here’s an example, in order for a man to eat food that is kept on the plate, given that the man exists; there must be some food on the plate; now this is a dependency. This is how you picture or model some real world things, so here’s the relationship, there must be food on plate for a man who’s sitting in front of it to eat, so man’s eating behaviour is dictated by the existence of food on plate, sorry for repeating myself but this is what a dependency is.

Now, in object oriented context we always need to and try to achieve as much generality as possible, in other words try to achieve good levels of abstraction for great quality code. Okay, what I mean by this, Let’s assume the man eats different type of food in different ways, Here’s an example; Imagine we have two types of food a bowl of noodles, and a packet full of biscuits.

Potions Class

Now this is my application class and here we have a particular way this man eats the noodles and biscuit, so each of the types have their own eat method. what this means is that; the man eats noodles in a different way so you create a new object of noodles and run its eat method when you eat noodles; when the man eats biscuits, you create a new object of biscuits and run its eat method.

Potions Class

Now, what we have here is; a particular implementation, and we even notice that in this application class we see that we have tied this type of food inside it, and we think forward; what if this application class needs to change; what if he needs to eat a new type of food. We obviously need to come back into the application class and write something like this

Potions Class

Now this isn’t a problem, since its just a single food type ;what if this keeps happening, that you keep getting the need to add new types every now and then. So what if you could create something that would automatically run the eat methods for any type of food? Now that is a great place to dive in for achieving good abstraction level, let’s try this using polymorphism; how do we go about it?

We can achieve this polymorphism using some kind of a parent class or something specific like an Interface class, so what we do here is use this Interface class to run this eat method, and when the application is running we send it different children of this parent class and at that time the children method would get executed, here’s what I mean by that

Potions Class

Now, the food types; Noodles and Biscuit inherit the FoodType’s eat method, so what good does this inheritance do? when I specify the Noodles type to the interface, the Noodles type eat method would run, and similarly if I specify Biscuit type to the interface , the Biscuit’s eat method would run. Perfect right? we achieved some kind of generality here.

Potions Class

so what are we doing differently here, we right now can just use foodType to run its eat method and through the different implementation type ( or Food types here like Noodles or Biscuit) it knows which method to override( which eat method to run looking at the type of food). Now to go even more levels of abstraction and trying to get rid of the way we are specifying the types to the interface, what will do ahead is this.

Potions Class

Now what we did here is that we are trying to we create a method where we just take in the FoodType and all this will do is that if the foodType is Noodles, it will execute the eat method of Noodles and so on to the other type, what is going on here is that the myEatMethod will just know that there is some kind of foodType that will be passed and it does have a eat method, and how its eat method behaves is not it’s concern as you can see, it sits unbothered, just send it a foodType it will run it’s specific eat method. But here’s the new problem, now even though the method looks like its generic, it still needs that FoodType to be created of a specific type and then sent into this method, which means at some point of your code in this class, or elsewhere you would need to create something like this

Potions Class

we would need this new instantiation of this FoodType( or specifying the FoodType). We would need to change this new instantiation, but we wouldn’t need to change any of the myEatMethod since its generic already, we still have this new Instantiation somewhere we need to take care of. So now whats happening is that every time we have a new instantiation, this is happening in our application class.

Potions Class

what we can try and achieve next is this.

Potions Class

Instead of a Specific Noodles object; we could have a generic FoodType object where some other class can give us this specific food types.

Potions Class

Here’s how we will achieve this. we create our Eating class

Potions Class

so what we have here is a member variable of the FoodType and make it private, and create a setter for it, so what this method is set up to do is that no matter what is the change in foodType that you will pass into this; it’s going to set the classes foodType to the one that is sent in, and you can use the eatFood method to execute its specific eat method inside. Notice that it doesn’t do any instantiation of a particular type, no “new” keyword being used. This is to say that the Eating class is going to assume that its going to get an instantiated foodType passed into it.

What we have achieved here is a good level of abstraction and removed the dependency of the type of Food from this Eating class, so if you pass a Noodles foodType; it will set the foodType to Noodles and then you can run the eatFood method to execute the eat method of the Noodles type specifically. So in some other class this is what’s going to happen

Potions Class

The advantage we have achieved here is that we went from this to this.

Potions Class

We have created a new generic Eating class which is now free from any specific type of FoodType, Now; any other class can send in its foodType and the generic eating class will know how to eat, the whole idea is that we don’t ever need to modify the Eating class for any new types of food anymore; its generic anymore, just pass the type and eat. We can now say that the Eating class is now generic and its dependency (of FoodType) is being injected by some other class, its not hardcoded into that class ( new instantiation in the class),we have decoupled the dependency, yeah about that..

Now you might have a question that what about this ..

Potions Class

yes, something needs to do this part of the work now, even though we achieved the dependency free Eating class, in the context of the spring framework, the other class is taken care by spring and you don’t really have to write this other class, just configure spring so that the dependencies you want to be injected into the right objects.

so, this was an explanation of what dependency injection is about, I will put up another post of how dependency injection happens in a much lower level so see ya then.

© 2022 by Vikyath Shirva. All rights reserved.