Last active
March 31, 2019 20:09
-
-
Save davidliu/6d8ad76fdc3ad1b2112f181663725e57 to your computer and use it in GitHub Desktop.
Example of dependency injection, referenced by https://www.reddit.com/r/androiddev/comments/aw860w/doesnt_dependency_injection_break_encapsulation/ehku2tt/
This file contains hidden or 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
| public class LocalProfileAPI implements ProfileAPI { | |
| public Profile retrieveProfile() { | |
| /*... retrieve from DB or something ...*/ | |
| return new Profile(); | |
| } | |
| } |
This file contains hidden or 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
| class Model { | |
| private ProfileAPI api; | |
| @Inject | |
| public Model(ProfileAPI api) { | |
| this.api = api; | |
| } | |
| public Profile getProfile() { | |
| return api.retrieveProfile(); | |
| } | |
| } |
This file contains hidden or 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
| class Presenter { | |
| private Model model; | |
| @Inject | |
| public Presenter(Model model) { | |
| this.model = model; | |
| } | |
| public void showProfile() { | |
| Profile profile = model.getProfile(); | |
| doSomethingWithProfile(); | |
| } | |
| private void doSomethingWithProfile() { | |
| /*...*/ | |
| } | |
| } |
This file contains hidden or 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
| interface ProfileAPI { | |
| public Profile retrieveProfile(); | |
| } |
This file contains hidden or 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
| public class RemoteProfileAPI implements ProfileAPI { | |
| public Profile retrieveProfile() { | |
| /*... retrieve from Remote or something ...*/ | |
| return new Profile(); | |
| } | |
| } |
This file contains hidden or 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
| class Screen { | |
| private Presenter presenter; | |
| @Inject | |
| public Screen(Presenter presenter) { | |
| this.presenter = presenter; | |
| } | |
| public void onSomeEvent(){ | |
| presenter.showProfile(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment