Generally we have different types of coding pattern and we choose coding pattern based on what type of app we are developing. But the common coding pattern that is recommended for beginners is MVP Pattern. Also we having other design patterns based on the android app development.
What is MVP ?
MVP stands for Model View Presenter, as mentioned earlier it is one of the coding pattern used for android app development.
In MVP single application is divided into three parts as:
Model in MVP is nothing but your data source. View does not talk to Model directly instead it commands Presenter to handle data for it and give the information back which can be displayed without any further modification. In other words we can say – this should basically be your full domain model, hopefully it will help making your domain model more “tight” as well, since you wont need special methods to deal with cases as mentioned above.
View in MVP contains two things Activity – android resource View – java interface Activity Implements the View and it injects itself (View interface) in the presenter so that presenter can talk to activity using view interface. Simply we can say :
- Activity – Android Resources
- View – Java Interface
Presenter acts as a middle layer between View and Data/Model. View(Activity) commands presenter to present something and presenter then takes data from the database/Model and gives back the presentable form of data to the View. View then takes care of displaying that data on the screen. And remember that Presenter is a plain java class it should not include any of the android components otherwise it will make the unit testing of the presenter hard.
If you wish to use database in the presenter then make activity create a database instance and inject it in the presenter. This will help you to mock the database while unit testing and will enable you to test the business logic.
MVP is definitely useful for Android. It helps to organise and unit test your code. And the best part is new people reading your code will be able to understand the code and will start contributing soon as they know what should go where
And also there are other patterns used in android like:
- Broadcast receiver uses Observer pattern
- Remoter service invocation uses Proxy pattern
- View and view group uses Composite pattern
- Media framework uses Facade pattern