Needed to make the video listing part of my application crash free, easy to maintain and extend. I had to turn to a design pattern and it was theI have never professionally written code in Android, so all the explanations below would be from a beginners perspective.
MODEL VIEW PRESENTER
.
This article is implementation of the ideas I learned from the below article. Please go through the below article to better understand the conceptshttps://www.techyourchance.com/mvp-mvc-android-1/
Blueprint for MVP Application
blueprint for mvp application
MVP Explained
Credit: https://www.techyourchance.com/
Like most architectural patterns MVP is open to a lot of variety and experimentation and its implementation can be ambiguous. So you can modify your implementation according to your needs as long as your implementation meets the below objectives.
- Separate the code for the view, which we show to the user like a list, buttons, labels, textbox etc from the business logic which is triggered on user interaction with the view which we call presenter. The data that we show in the view should be provided by another separate module which we call as model. In turn code becomes readable, understandable and maintainable.
- The communication between the view and the model needs to take place through the presenter i.e. The view and model cannot have reference of one another.
- Using this design pattern we should be able to make changes in one components without having the need to change any code in the other two components. Also in case we want to completely replace the view or model with another implementation, that should be possible as well. for example you can use content provider to get list of videos, that tomorrow should be replaceable by sqLite database without change in any other part.
- Maximize the code that can be tested with automation.
The android framework makes it really easy to start development of any idea that you have in mind, but this structure of code very soon becomes unmanageable with a lot of spaghetti code and classes with different functionalities all mixed into one activity/fragment class.
Now, the android activity seems to give the functionality of view since we do things like setContentView() in OnCreate(). Activity at the same time maintains the android lifecycle events like onCreate() onResume() etc which would be the business logic i.e. presenter.
Another simple example to show how they are entangled
- Register listeners for user’s interactions with application’s UI — View
- Perform actions in response to user’s interactions with application’s UI — Business Logic
Most of you would agree that debugging an Activity code with several lines of code registering UI components and at the same time also having a few lines of business logic embedded here and there is a huge pain.Now, on the basis of Single Responsibility Principle, we need to assign Activity the role of a
View
or a
Presenter.
I decided to use
Activity as the Presenter,
because activity has the
context
(which is the god Object that provide access to most platform’s features). This makes Activity a natural choice for keeping the responsibility for business logic. After all we want keep the view to be dumb module that only has the functionality to load the UI elements. For more detail on this topic, you can go through this article explaining why activities are not UI elements.
Implementation of MVP in my App
The complete code is available on github. Please check out android-video-listing-mvp
Author: Nitin Agarwal
Source: https://android.jlelse.eu/android-mvp-for-beginners-25889c500443
Comments
Post a Comment