Hi everyone.
Let's continue with the inversion of control.
Okay.
The inversion of control is just a principle and it will achieve the loosely coupled
Now the whole purpose of inversion of control okay is resolve the "tightly coupled" and archive.
Okay.
And archive a loosely coupled
And in this video let me show you what is the tightly coupled okay.
Now for example, I will build the application inside the TypeScript playground.
Okay.
I will show you the concept.
In here. I will build the application like I drive a car.
Okay.
To build this application I will create a two class.
The one class will be the "person".
And the second class will be the "car" okay.
Now in the create person class.
And the car.
Now.
Right here.
For example, in the car I will return.
Sorry I will have a method.
Will get name.
Okay.
Get name of the car.
I will return the name of the car.
Let me see Lamborghini right here.
Now.
And inside the person, I want the drive a car
Right.
I will have a method called drive.
Right?
How I can drive a car.
That's a question right now.
In order to drive the right car, we must be initial
Okay.
We must init the car inside the person class.
The car.
Car right here and right here I will
I will console.log(this.car.getName())
Actually
I will cut that and use backtick
I drive a car right here.
And right here.
I must use a new car.
I must init like this
Something like this.
And now let me show you how I can run this function.
You can see in here I will create a p1 equals new person.
new Person()
And I will call p1.drive()
And call the function.
Now let me see if I run you can see, it say I drive a Lamborghini.
But this is clearly a "tightly coupled" because how I can drive another car, right.
For example, later I will duplicate the car here.
I will go, this is the car x
Like this, okay
And this will be the some name of the car.
How I can drive a different car, right?
In order to to achieve that, I will change the car here and the car here.
Right.
And if I run.
If I run now, you will see it will show like this.
But in the real world we don't want to do that.
Right.
We don't want to update the class.
And for that reason, we will use the "loosely coupled" with the idea of inversion of control.
Now let me see what is the inversion of control.
Right here, instead of use initial a new car.
We must use the constructor to initial that car.
And right here will be this.car
And in order to accept in the into the car here, we must use the car.
Car right here
this.car = car
The car here is the parameter.
And here right here will be the property of the class.
I can change the name.
So a little bit clear okay.
You can see, I will undo
Now you can see right now it will have the error because it's expect the one argument.
Right.
And for that reason we need to initialize the car with a new car.
And I will pass the c1 here into the person.
And let me run.
Let me run.
If I run now, you can see it will drive a Lamborghini.
But the one more question right now is how the p1 can drive a car
X right here, right?
Because you can notice in the constructor I used a car here.
It's clearly a will be the class of the car.
Right now if I create a c2 equals a new CarX.
And if I pass the c2 here, you can see it's a work or not.
Now let's pause the video and think about does it work or not?
Okay.
Let me show you if I pass the c2 into p1
Now you can see it doesn't have any error right in typescript.
If I run this work.
But why?
Clearly the car how it can be work right here.
Now you might think because this is same it's same name.
But for example, if I have something like this get.
X
If you see now you can say it clearly different method, but it also work, right?
Why?
Why it work.
Now let me show you the reason it work.
If you look into the dot JS right here, this is compile.
It will compile the TypeScript into JavaScript.
And if you see when it compile, it will ignore the data type right here, right?
So for that reason it works, but it only works in the TypeScript.
If you write this code into the Java or C#, it will clearly not work because it's a different data
type.
Okay.
It's only work because this is a.
TypeScript.
Hopefully this makes sense.
And for example, if it not work how I can do that.
Okay I will assume this not work because this is a right here.
And in order to do that, in order to achieve the "loosely coupled" very clearly "loosely coupled"
something call interface.
Okay.
I will create a interface call Vehical
And inside interface I will define the getname.
Okay.
It will return a string.
Okay.
That's it
And in here I will implement in the class.
I will implement the vehical
And in the car here I will also implement the vehical.
And.
In the car here I will change the car into the vehicle.
Hopefully this makes sense because we used the interface.
So it will work if you write this code right here in the Java or C sharp.
And this is a "loosely coupled"
Okay.
But in TypeScript
We usually not.
Not write this
way
Because in the in the laser it will compile the code into .js and it will remove the interface.
You can see this doesn't have any interface in the JavaScript right here.
Right.
For that reason in the Nest.js and in the TypeScript, we will not use the interface right here.
And it will be work
But now, let's clearly
This only works because this is the JavaScript.
Hopefully it's make sense
Right.
And I assume you already understand what is the "tightly coupled" and "loosely coupled"
Okay, I will see you in the next one.