I was reading an article recently about the SOLID principles and I can't seem to understand the example about the Dependency inversion principle (DIP). The article gave the following example. First the incorrect way, then the correct way.
INCORRECT WAY
class Login {
login(googleLogin: any) {
// some code which will be used for google login.
}
}
Makes sense, the article explains that if the client wanted to add a Facebook login tomorrow, you would have to modify the Login class, which should not be necessary with DIP. Dependent on abstractions and not concretions. Understood.
CORRECT WAY
interface ISocialLogin {
login(options: any);
}
class GoogleLogin implements ISocialLogin {
login(googleLogin: any) {
// some code which will be used for google login.
}
}
class FBLogin implements ISocialLogin {
login(fbLogin: any) {
// some code which will be used for fb login.
}
}
class Login {
constructor(private login: ISocialLogin) {}
onLogin() {
this.login();
}
}
Now, in this example.. how do we know which class the Login class is going to use? I could see if in the constructor you specified GoogleLogin vs FBLogin, but just implementing the interface is confusing to me. Can somebody please explain? Thanks in advance.
Editing for additional question...
I'm still missing something basic I think. In this example (Angular specific), say I have the following UX. Inside of a LoginComponent, I have a GoogleLoginComponent and a FacebookLoginComponent. In both of those child component examples, I would use the GoogleLogin and FBLogin classes, correct?