Last active
August 21, 2019 03:00
-
-
Save hakaneroztekin/136f354e41f0b3ef7170da5e73ec5562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the example above, the userLister is not initialized | |
class DependencyProblem { | |
private UserLister userLister; | |
// gets users from DB and renders to the view (just a trivial example) | |
public void render() { | |
List<User> users = userLister.getUsers(); | |
view.render(users); | |
} | |
} | |
// If we initialize it like below, we increase coupling between classes (tight coupling) | |
// UserLister userLister = new UserListerDB(); | |
// Also, if we want to change it, we need to use UserListerCommaSeparatedFile(); | |
// This change is no problem for a small project but a nightmare for a huge project | |
// Inversion of Control Solution | |
class SpringIOC { | |
// Just adding the annotation is enough. Spring manages the dependency (DI). | |
@Inject // this and @Autowired are almost identical | |
private UserLister userLister; | |
public void render() { | |
List<User> users = userLister.getUsers(); | |
view.render(users); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment