MVP - What is where?

Asked

Viewed 197 times

1

I have a question about the MVP. In my studies I understood that in M = Model there is everything that has to do with business rules and codes for handling business-related data, bank access, Adapters etc. In P = Presenter is the presentation, or the interaction between the Model and the View to manipulate, chewing what should be shown. And the view is the view!

But on Android we have for example native android methods like onRequestPermissionsResult, Onclicklistener listenerTvAddDetailsRotulo, onActivityResult. These items would be in the presentation or in the template?

Or by being native android items I leave as this on Activity same?

  • You have no alternative but to leave them in Activity. You can/You must delegate the answer to them to code in the Presenter.

1 answer

1

The model is responsible for the data that will be displayed in the user interface. We could consider as a model, besides the data, any logic of manipulation and access of this data.

The view, normally implemented by a Activity(can also be a Fragment or any UI element, depending on the structure of your application), will contain a reference to the presenter. The presenter can be created by Activity or supplied via dependency injection. The sole responsibility of the View is to call methods in the Presenter every time the user interacts with it.

The presenter acts as an intermediary between the view and the model. It takes data from the model and returns to the view. But, unlike typical MVC, it also decides what happens when user interacts with the view.

These definitions above were taken from article by Antonio Leiva.

As for the methods onRequestPermissionsResult, OnClickListener, onActivityResult; shall be maintained in their respective activities or fragments, forming part of the view in relation to MVP, which are also in Android’s own classes. Already the method listenerTvAddDetalhesRotulo, which you probably invented, depending on the context, you can get into presenter or model.

If you are interested in good practices when it comes to Android, I recommend reading Best Practices in Android.

  • For example, if I have in Activity a method called public Boolean check Fields(Car car) {...} that checks whether fields are filled and returns a Boolean to indicate the response of this check, it should be in the model or presenter?

  • In this case, it will depend a lot on what the method will do. Could insert in the Model or Presenter. VerificarCampos(), It would be something like validation right?! Then you could insert it in the Presenter. Look at this article, you can give another clarification; http://blog.karumi.com/interfaces-for-presenters-in-mvp-are-a-waste-of-time/

  • but the checkCampos I need Edittext for example to check if they are filled but they are in Activitiy, as I will check them inside the presenter?

  • 1

    You can pass the Edittext by parameter, for example Check fields(editTextSelf); And inside you do the validation. This if you want Edittext to have some reaction, such as changing the background color if it is not validated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.