Description
Architectural components of an app should have a single, well defined, responsibility. As a component grows bigger, it should be split up. By following the single responsibility principle, the app architecture naturally supports the structure of developer teams and development stages. Additionally, monoliths are detected in the early stages and modules become testable in isolation. Finally, if the app is built using Gradle, modularization can improve the performance of the build process and ease the development of Instant apps . It is important to notice that, while modularization may imply little effort if considered early in the project, it might become an extremely expensive process in later development stages
Example
We created a simple MVP login Application. The AuthComponent has one responsibility: Inject the specified modules after login. There are two activities that can take place after login:
- Get a greeting
- View other users
So for both of these activities we create a component. A GreetComponent and a ViewUserComponent. The GreetComponent injects everything that is needed for the GreetActivity. The ViewUserComponent injects everything that is needed for the ViewUserActivity.
We will only look at the GreetComponent. The GreetComponent uses the GreetModule. Which should provide everything the GreetActivity needs.
As we can see the GreetActivity needs a GreetPresenter. The GreetPresenter needs a GreetView and Credentials.
So all of this needs to be specified in the GreetModule.
The GreetPresenter calls the showMessage(mCredentials.getPersonalizedGreeting()) on the GreetView. This way each Architectural component has a single well defined responsibility. If we did not create a specific AuthComponent for this, we had to put everything into the AppComponent. Which would then have too many responsibilities.