Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP Android - How many presenters?

Tags:

android

mvp

I have a quick question. I am trying (and struggling) to design my application with the MVP design pattern.

Can I ask, for each view (activity, fragment) should I have a separate presenter class?

There aren't very many resources I can see online, that clearly with samples illustrate the MVP. Could anyone share if they have some?

PS I am also using RecyclerViewAdapter in this app so any pointers on that would be appreciated

Thanks in advance

like image 465
DJ-DOO Avatar asked Dec 11 '15 15:12

DJ-DOO


People also ask

What is presenter in MVP Android?

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern which mostly used for building user interfaces. In MVP, the presenter assumes the functionality of the “middle-man”. In MVP, all presentation logic is pushed to the presenter.

Can view have multiple presenters?

View (Activity) can have multiple Presenters . In case of having multiple CustomViews for Activity , you can have one giant Presenter or Presenter per each CustomView .

What does presenter do in MVP?

Presenter: The last part is the presenter, which handles UI updates based on changes to the data model, and also processes users inputs. The presenter will contain much of the business code and replaces the controller from MVC.

Why is MVP better than MVVM?

MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing. All my projects(written in Kotlin for Android app) are based on MVVM.


1 Answers

Though old, this is very interesting question. Since nowadays MVP/MVC/MVVM is kind of "buzz-words" in Android community, this question deserves a more complete answer (IMHO).

Short answer:

Single presenter can be used with multiple views

Long answer:

In general, there is no one single definition of MVP/MVC - there are many approaches to implementation of these architectural patterns. You did not provide the definition of "your" MVP, therefore I can only guess what you have in mind.

That said, there are some "best practices" in Object Oriented Programming which any implementation of any architectural pattern should (ideally) take into consideration.

What you ask is whether you can reuse one presenter implementation with different views, right? Let's look at this question through prism of SOLID principles.

"L" stands for Liskov Substitution Principle (LSP). It is one of the most misunderstood principles in SOLID, but the general idea behind that principle says the following:

LSP: if a piece of code works with object of class A, it should also work seamlessly with objects of any subclass of A (i.e. subclasses must be usable instead of A everywhere)

Example of LSP violation in Android is Context: sublasses of Context (e.g. Application and Activity) are not equivalent. Some code that requires Context can work seamlessly with Application, but if you pass Activity instead, a memory leak will occur (this is very widespread bug in Android applications, which is caused primarily by violation of LSP by Google's devs).

Back top your question. I'm assuming that your presenter looks like this (note the interface for views):

public class SomePresenter {

    /**
     * Views bound to this presenter must implement this interface
     */
    interface SomeView {
        void doSomething1();
        void doSomething2();
    }

    public void bindView(SomeView someView) {
        // view binding logic
    }

    // more presenter's methods

}

LSP states that any class which implements SomeView can be usable with SomePresenter. The presenter shouldn't care whether the implementation of SomeView passed to it is Activity, Fragment, or, maybe, just a mock for unit test.

So, the full answer to your question is: one presenter can be reused with different views, as long as the presenter doesn't depend on particular implementations of views, but only on their super-class.

Additional information:

I would guess that you asked your question because, on the one hand, you felt that a single presenter should be able to work with different views (just think about A/B testing of different UIs), but, on the other hand, the fact that views are Activity and Fragment made you feel uncomfortable with this thought.

My personal opinion is that in MVC/MVP neither Activity nor Fragment should be views. The reasoning behind this claim is summarized in this post: Why Activities in Android are not UI Elements.

enter image description here

I also suggest you to take a look at a different approach to implementation of MVP in Android - if you'd use this one, it would be evident to you that a presenter should be able to work with different views, and you wouldn't have this feeling of "something doesn't feel right".

like image 113
Vasiliy Avatar answered Oct 20 '22 03:10

Vasiliy