DJ-DOO
DJ-DOO

Reputation: 4781

MVP Android - How many presenters?

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

Upvotes: 16

Views: 3296

Answers (3)

Renaro Santos
Renaro Santos

Reputation: 403

Your Activity/Fragment should have 1 presenter. I like to make all my activities to extend from BaseActivity, and then, I make this BaseActivity requires a Presenter, see this example:

    public abstract class BaseActivity<Presenter extends BasePresenter> extends AppCompatActivity {

    protected Presenter mPresenter;

    @NonNull
    protected abstract Presenter createPresenter(@NonNull final Context context);

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter(this);
        mPresenter.onCreate(savedInstanceState);
        }
    }
    // other lifecycle methods

And then, create an abstract BasePresenter

public abstract class BasePresenter {

protected BasePresenter() {
}

@NonNull
public static BasePresenter nullPresenter(@NonNull final Context context) {
    return new BasePresenter() {};
}

@CallSuper
public void onCreate(@Nullable final Bundle savedInstanceState) {
}

Now, when creating a activity, do as follows:

public class MyActivity extends BaseActivity<MyActivityPresenter>{


@Override
MyActivityPresenter createPresenter(@NoNull final Context context){
     return new MyActivityPresenter(all, Your, Dependencies, Here);
    }
}

Now watch this video and understand the responsabilities of the Activity/View within the MVP.

Upvotes: 1

Vasiliy
Vasiliy

Reputation: 16228

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".

Upvotes: 19

Gewure
Gewure

Reputation: 1268

The Model-View-Controller design emerged very early in softwaredesign and was originally used for such things as an button element. You use the MVP(basically the same as MVC) in order to achieve an architecture which is modular and thus easy to maintain, spliting the representation from the logic.

Given your question i think you want indeed one Class per View. This would be the most common approach.

http://antonioleiva.com/mvp-android/ gives a theoretical overview of the MVP.

Upvotes: 5

Related Questions