‫Hello, my friends, and welcome to Dependency injection course.
‫In this video we will continue talking about dependency injection, how to use it, why we use it and
‫when we use it.
‫So we have talked about this topic in the previous video and we took an example in this video.
‫I'm going to take an example in order to clarify all concepts about this topic, to start building and
‫diving into more and more advanced topics in side dependency injection like dagger, hilt coin and when
‫to use these proper libraries.
‫So if you want to learn coding and start from zero to hero, you came to the right place.
‫Hit Subscribe to our YouTube channel and rate us five stars on Udemy so you get all courses free.
‫What is dependency injection?
‫Before we will talk about this dependency, we will make an ordinary app that we have learned how to
‫make it in Java or Android.
‫I have created a class called car.
‫This car have an engine.
‫Like a parameter.
‫It depends on an engine.
‫And this engine.
‫This engine.
‫Sorry instance would make a start a calling the start method.
‫So when the the user needs to start the car, he needs to call the engine instance and go and start
‫the engine.
‫The engine.
‫By using start engine method, I would go to the engine method and the class.
‫This engine class contains a start method.
‫The start method will give us the A logcat message telling us that the engine has been started.
‫So till now I have created a car.
‫A car depends on making a start for the engine.
‫So when we the user, want to start the car, the car should start the engine.
‫This is what we have written.
‫So this is the engine having the start method and the car method has the start car method that we should
‫call the start engine method.
‫Let us test this functionalities and test these classes inside the Oncreate method.
‫I am making them on Android studio in order to to keep the concepts in Android studio.
‫I can make them on Java.
‫They are the same, but I prefer making them here in order to to understand the functionality faster
‫later on.
‫I have created an instance of the car by making a new car.
‫So I'm just making an object of the car and calling the start.
‫Calling the start method of the car.
‫So the car instance would go and call the start.
‫The method and inside the start method it contains an engine object that would be started.
‫The engine.
‫Let me run the app and see what are the results.
‫So in this example, the car will depend on the engine.
‫So when we need to start the car, we need to start the engine.
‫And when we need to create a car, we need to create an engine.
‫So.
‫This is what we have been talking about.
‫So this is you see guys in the logcat here, we have engine started tag.
‫So tag if we search for tags, we.
‫We will get this.
‫This is the lookout.
‫Okay.
‫And if we search for the tag, we get the engine started.
‫So it's very simple.
‫This is called hard dependency.
‫So we didn't made the dependency injection.
‫This is what we are going to improve, what we are going to use and dependency injection for for.
‫Okay, guys.
‫As I told you, this is an example of hard dependency.
‫Now we are going to make a dependency injection and see the difference.
‫But before we start, let me ask you some questions.
‫I will recall the three ways for a class to get an object it needs.
‫So there are three ways for a class to get an object it needs.
‫A class constructs the dependency it needs as we have created here.
‫The first one is the class construct, the dependency it needs.
‫When we have, we need to create a car.
‫We need to create an engine.
‫So whenever you need to create a new car, you the car class will go and create a new instance of the
‫class.
‫Engine.
‫So the car, it creates its own dependencies, its own objects.
‫This is, as I told you, hard dependency.
‫And it's one way of creating and or a class to get an object.
‫So class creates its own objects of other classes.
‫This is.
‫As this figure.
‫You see guys that we have created a car.
‫It depends on the engine.
‫So whenever we need to create an instance of a car, we the car class creates an engine instance and
‫depends on it.
‫Okay, so back to here.
‫The second way that the class receives objects it grab it from somewhere else, some Android APIs such
‫as context getters and get system service work this way.
‫So this is the second way that the class get an object.
‫The first one is constructing its dependency.
‫As with this example do, the second is grabbing it from somewhere else.
‫This is not very important for us in the dependency injection and the last way that they use the class,
‫get an object from it or what we need from object is having to supplied it as a parameter.
‫The app can provide these dependencies when the class is constructed or passed them into the functions
‫that need each dependency.
‫This is an example that the car constructor would receive engine as parameter.
‫So we are going to make and transform this hard dependency.
‫An example to dependency injection.
‫With this approach, you take the dependencies of class and provide them rather than having the class
‫instance obtained them self.
‫Okay guys.
‫So the as I recall that this car class would make every time we create an engine and an engine class
‫and go and call it every time the class, the car class would be created, an object from it, an object
‫of engine would be created.
‫And this encoding may be problem.
‫A very big problem.
‫Okay.
‫So.
‫Let me.
‫It give you a question and see what we why we need dependency injection and why how what things does
‫dependency injection solve?
‫Okay.
‫Whenever.
‫You need to create a car.
‫You need to create an engine for it.
‫The car class would create an engine for it.
‫So the question is here.
‫Car and engine are tightly coupled.
‫An instance of car uses one type of engine and no subclasses or alternative implementation that can
‫easily use.
‫If the car were to construct its own engine, you would have to create two types of car instead of using
‫the same car for two types of engine.
‫So guys, the question if you need to create a car with two engines, one electric and one for gas.
‫What is the what is the solution?
‫So I need you to answer it, to think about it.
‫Okay.
‫So if I need again, if I need to create a car with two engines, it is impossible in this way to create
‫a car with two engines because.
‫Every time I need to create an engine or I need to create a car, it creates one engine and it's impossible
‫to create two types of engine, one electric and one gas.
‫And this is a very problematic and very, very hard to make and it's impossible to make in this logic.
‫So here's come the dependency injection Again, another question.
‫If I need if I have created a car with an A gas.
‫Engine.
‫This car object is called car 111.
‫If I need to replace the gas engine for this car.
‫111.
‫I should create a new object of a class, and I need to create a new engine so it's impossible to modify
‫the created object or test it.
‫The hard dependency on engine makes testing more difficult.
‫Car uses a real instance of engine, thus preventing you from using a test double to modify engine for
‫different test cases.
‫So guys, the dependency injection is the solution.
‫Okay.
‫We have the solution for multiple types of of engines and we have the solution for the testing.
‫Later on, we will make a new core with new topic and playlist and course about unit testing.
‫So unit testing in Android is very important and we came across it in this in next courses and the dependency
‫injection is very it's the basic and the basis of unit testing.
‫Imagine that every time you need to make a test for your class or your you need to create a real object.
‫So dependency injection is the solution for unit testing and it makes your code more and more cleaner
‫and more and more usable.
‫Let me show you how to transform your code into a dependency injection.
‫And then later on in the next course, in the next videos, we are going to use the third libraries.
‫And in this video we will see why we are going to use like dagger or hilt or these classes, these third
‫party libraries.
‫Okay.
‫So again, back to our course back to our video here.
‫I will modify this and I will remove this private.
‫New engine.
‫I will remove making every time an instance of the engine, every time an instance of car would be created.
‫So for that I make an end constructor injection here.
‫And every time passing passing the engine here inside the constructor of the car, I'm not making every
‫time a new engine instance, I'm just passing the objects.
‫I'm just passing the objects of the engine for the car object.
‫Okay.
‫In this way, we have eliminated the dependency of the car on the engine.
‫We can pass the engine every time.
‫I need to create a car.
‫So.
‫Engine class would be also the same.
‫It's very simple, guys.
‫The dependency injection may be a very complex and advanced thing, but you see, guys, I have created
‫a very good idea.
‫I have like make you understand it very well.
‫So just removing let me show you what I have did.
‫I have just removed the new engine so every time the car would be created, it will not create it.
‫It will not create the engine instance.
‫And for that I will pass inside the constructor of the car, the engine.
‫I am just passing the object of engine to the constructor of the car.
‫This is the dependency injection.
‫Let us go to the main activity and you see guys there is an error here telling us we need to create
‫an engine.
‫So it's very simple guys.
‫You can now create an engine equal to new engine here.
‫Okay.
‫Engine.
‫Okay.
‫And I'm passing it.
‫Okay.
‫This is very simple, guys.
‫Now I can pass for this car.
‫I can create for it a gas engine.
‫A fuel engine, electric engine, a jet engine, whatever you want.
‫Guys, So this is the functionality.
‫This is the importance of dependency injection.
‫Like making this car is not dependent on the engine, it just passing it into the constructor as a parameter.
‫So let me test our app.
‫Let me run our application and see if the engine would be started or not.
‫Okay.
‫I will restart the app.
‫Reinstall it.
‫I hope you understand the concept of of this.
‫And I need you to make some practice about this.
‫Open your Android studio and try to make your codes.
‫Try to make this example.
‫And I am listening for you in the comments and the inquest in the questions below in the comments.
‫So I'm happy to ask me order me just making a comment like thanks or whatever you want.
‫Or you can you can make some.
‫You can get my faults and wrong things or the bad things.
‫You you hate me and you can comment it.
‫I'm I love listening to your comments back to Android studio here we have the engine has been started
‫and our application is now a not dependent on the engine.
‫So I will show you a diagram here you see guys the previously diagram of the engine here has been transformed
‫in our application to this this diagram.
‫The car class is not dependent on engine.
‫The dependencies of the car are not shown and we have made it not dependent on the engine.
‫It is the dependency injection.
‫Now we can test the car with gas engine.
‫Fuel engine.
‫Jet engine.
‫Supersonic engine.
‫Electric engine.
‫What you want, whatever you want, any engine you can test the car.
‫And this is the very concept and very advanced concept that you should learn for unit testing your application
‫in order to advance your coding coding skills and make you professional coder.
‫I hope you benefit benefit from our videos.
‫If you have any questions.
‫I am listening to you and I hope you like subscribe and share on YouTube, YouTube and Udemy.
‫Rate us five stars.
‫We will start in next course in next videos.
‫Using the dagger hilt and much more.
‫Thank you guys.
‫I hope you understand everything.
‫Bye bye.