Model-View-Presenter: Android training in Chandigarh
There are so many articles and examples on MVP architecture and various implementations.
Continued efforts are being made to optimize Android in the best way by the developer community.
When adopting this pattern, you can see that there is an architectural choice, the code base is
changed, and the way of accessing the new function is changed (for improvement). You also need to
know that you must face common Android issues like lifestyle in activities.
- Should I rescind the presenter's position?
- Should I be a presenter?
- Should the presenter have life cycle?
In this article, Android training in Chandigarh going to put down a list of best practices to follow in
order to:
Solve the most common problems (or at least those people in my personal experience) using this
pattern
Maximize the benefits of this pattern
First of all, let's describe the players:
Model-View-Presenter
Model: This is an interface responsible for data management. The responsibility of the model is API, data caching, database management etc. This model is also an interface that
communicates with other modules responsible for these responsibilities. For example, if you are
using a repository pattern then the model can be a repository. If you are using clean architecture,
then the model can be an interactive.
View: It is the responsibility to present the data in a manner determined by the presenter.
Views can be implemented with things that can perform operations such as displaying activities,
fragments, arbitrary Android widgets, or ProgressBar, updating TextViews, and creating
RecyclerView.
If you are following the guidelines from Android training in Chandigarh and you can not like them,
then I will try to tell why these principles are right for us.
Make View dumb and passive
One of the biggest problems of Android is that it is not easy to test thoughts (actions, pieces, ...) due
to framework complexity. To resolve this problem, you should implement the idle view pattern. The
implementation of this pattern, using the presenter in our case, reduces the behavior of the scene to
the full min by using the controller. This option improves testability dramatically.
Make presenter framework-independent
To improve testability, make sure that the presenter is not dependent on the Android class so that
valuable principles are really effective.
Since abstracting presenters from implementation details (ie, the Android framework), you can write
tests that are not instrumented for presenters (even without using Robolectric). Without emulator.
What if I need the Context?
Well, get rid of it. In such cases, ask yourself why context is necessary. You may need a context to
access shared configuration and resources. But you should not do it at the presenter. You need to
access the resources in the view and the preferences in the model. These are just two simple
examples, but in most cases, Android training in Chandigarh sure they are just a mistaken
responsibility issue.
By the way, if you need to decouple objects, the principle of dependency reversal is more useful in
such cases.
Define a naming convention to separate responsibilities
Presenters can generally have two categories of methods:
The load More () method is called when the user scrolls to the end of the list and the presenter loads
another page of results. This choice means that the view will recognize that when the user scrolls to
the end, the new page needs to be loaded. In order to "reverse" this logic we have attached a method
onScrolled End () which allows us to decide what to do for a specific presenter.
What I'm saying is that in the "contract design" phase it is necessary to determine who each user
event, corresponding action and logic belongs to.
Do not create Activity lifecycle-style callbacks in Presenter interface
This title means that the presenter should not have onCreate (...), onStart (), onResume () methods
and their dual methods for several reasons:
In this way, the presenter is combined with the activity lifecycle, in particular. What if I want to replace
an activity with a fragment? When should the presenter.onCreate (state) method be called? OnCreate
(...), onCreateView (...) or onViewCreated (...) of a fragment? What happens if We use a custom view?
Presenters should not have a very complex lifecycle. The fact that the major Android components
are designed this way does not mean that you need to reflect this behavior anywhere. If you have the
opportunity to simplify, do it.
Activity Lifecycle Instead of calling a method with the same name in a callback, you can call the
presenter's action. For example, you can call load () at the end of Activity.onCreate (...).
Provide a cache for the Model to restore the View state
In my opinion, in order to solve the problem of "restoring state", trainer of “Android training in
Chandigarh” need to change a little bit of the architecture of the application. A wonderful solution to
this idea was proposed in this article. Basically, the authors suggest using a repository-like interface to
cache network results (to manage data scoped to applications rather than applications so that they
can withstand changes in orientation It is aimed at).
This interface is a little smart model. The latter should provide at least disk cache strategy and
in-memory cache. Therefore, even if the process is destroyed, the presenter can use the disk cache
to restore the state of the view.
Views relate only to the request parameters required to restore the state. For example, in our example,
we just save the query.
Well, you have two choices:
When the presenter calls repository.get (params), this behavior can be abstracted at the model
layer so that the data source will return it if the page is already in the cache, otherwise the API
will be called To make it
If you manage this inside the presenter, another method of contract is added and the state of the
view is restored. restore (params), loadFromCache (params) or reload (params) are different
names describing the same action.
This is my knowledge of Model-View-Presenter applied to Android. I learned from my own mistakes,
the experiences of others and my experience. Android training in Chandigarh have always found
the best approach.
Comments
Post a Comment