9

I try to understand what the best way is to program a "clean" Android app. I found an interesting article about it where they use a kind of a 3 layer architecture.

Now I read that android uses the MVP design pattern and try to understand this. The way I understand the MVP principle (in the android context) is that:

  • the Model layer are the logic classes
  • the presenter are the classes that are "linked" to an XML (the activities)
  • the passive view are the XML's.

Is this correct?

When this is correct, is it then also correct that the 3 layer model fits perfectly in the MVP principle because:

  1. the presentation layer from the 3 layer architecture fits in the presenter layer of MVP
  2. The business and the data layer from the 3 layer architecture fits perfectly in the model part of the MVP?

I hope my questions are clear because it is a complicated topic for me.

Maybe this clears up my vision (way of thinking) about this... Maybe this clears up my vision about this...

Cœur
  • 37,241
  • 25
  • 195
  • 267
CodeNinja
  • 836
  • 1
  • 15
  • 38
  • I wouldn't say MVP works perfectly for *every* application of android. It depends entirely on what you want to do. I have used MVP for my own android app and I think it cleans up the code heavily, from having the bulk of the code in your Activities and Fragments. That being said, there was an interesting talk from Yigit - https://www.youtube.com/watch?v=BlkJzgjzL0c which doesn't seem to use MVP. Another really interesting article can be found here about MVP: https://medium.com/ribot-labs/android-application-architecture-8b6e34acda65 – riggaroo Dec 28 '15 at 14:36
  • Thanks, i will watch the video's tomorrow. But do you know if that what i wrote about MVP is correct? – CodeNinja Dec 28 '15 at 14:39
  • Another really good example of using MVP can be found here : https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#0 . Which does describe how to implement MVP. I would say the View is the Activity/Fragment, the presenter is a seperate class that performs actions on the view and the model is the POJOs related to the view. – riggaroo Dec 28 '15 at 14:39
  • I think the business layer can be in the model or the presenter layer, depending on what seems more suitable. – Eric Brandwein Dec 28 '15 at 14:55
  • Ok, i can find me in that, makes sense – CodeNinja Dec 28 '15 at 15:14
  • There is a MVP sample application that you can check here: https://github.com/renaro/tinder-like-app , also you can understand more about MVP in this video here: https://www.youtube.com/watch?v=iXDAcWEhYSk&t=5s – Renaro Santos Mar 14 '17 at 01:30
  • I've been thinking about this for a while and have started writing a blog on the topic if you're still interested: http://cj65535.blogspot.com.au/2017/03/a-simple-mvp-framework-for-android.html – C B J Mar 30 '17 at 12:17
  • @CJames I dont need it anymore but i checked your blog, looks nice. For everyone else, the link is out dated, the new link is: https://notesofapragmaticprogrammer.wordpress.com/2017/04/06/building-a-simple-mvp-android-app-from-the-ground-up/ – CodeNinja Mar 22 '18 at 11:40
  • 1
    @RoDo Thanks. I've been working on a new framework trying to take the sting out of Android MVP development. It's at https://github.com/cjsoftware-lib/ucsFramework if you're interested. It uses annotation processors to support automatic UI/Background threading and UI state preservation. – C B J Jul 07 '18 at 02:55

7 Answers7

11

first thing I wanted to clarify is that MVP or any other pattern for that matter is no specific of Android dev and, can be applied to any other framework.

I think you got the gist of it:

  • view is usually implemented by activities and fragments and will contain the reference to the presenter
  • the presenter is that middle man between the view and model. Retrieves data from the model and returns it already formatted to the view so it doesn't need to do anything else but display it.
  • the model can be seen in a simplistic way as the "data provider". It can be as complex as you want, using internal db, lots of clases etc.

If you are interested in Android apps architecture I suggest you watch this talk given at Android dev summit this year. It is one of the bests I've seen

https://www.youtube.com/watch?v=BlkJzgjzL0c

Aitor Viana
  • 933
  • 6
  • 15
  • Thanks, this explains a lot to me and i think my vision of the MVP in the Android case is right. Now i'am curious if my association with the 3 layer architecture is right too. – CodeNinja Dec 28 '15 at 14:54
2

Even though this question has an answer, I don't think this answer is complete by any means.

MVP is a general concept which can have many various implementations, some of which differ substantially. Moreover, the concept itself is very ambiguous - different people can have different concepts in mind when they say MVP. One of the most widespread ones is shown in the below picture:

enter image description here

Regardless of implementation, the general definitions of MVP components are:

  • Model: abstraction of "application state storage". The definition of what the "state" is and how it is stored are implementation details. Model implementations should not have dependency on View or Presenter.
  • View: abstraction of "user interface". The definition of who the "user" is and how it interacts with the View are implementation details. View implementations should not have dependency on Model or Presenter.
  • Presenter: encapsulates application's business logic. Presenter processes user input events received from view, and alters application's state stored in model in response. Presenter also processes changes of application's state stored in model and updates view in response. Presenter usually depends on both the View and the Model.

If you need more information about MVP in context of Android development, you can find it in this post: MVP and MVC Architectural Patterns in Android

Vasiliy
  • 16,221
  • 11
  • 71
  • 127
1

Important issues which need to be addressed while implementing MVP in android are activity leaks which cause memory leaks and app crashes due to background process updating closed activity.

Due to presenter having reference to activity, if presenter can't be garbage collected, activity will stay in memory.

Both of the issues can be solved by using life cycle methods of activity or fragment and releasing resources in those methods so that memory leaks and app crashes are prevented.

Cleaning up of resources related background work can be easily implemented using RXJava, for more information about MVP and MVP with RXJava, see http://www.zoftino.com/android-model-view-presenter-mvp-pattern-example

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31
1

Here is simplest way to implement MVP pattern in your application android_mvp_login_sample

ramkrishna kushwaha
  • 380
  • 1
  • 6
  • 17
0

As you have come to know basics of Clean Architechure. The following example depicts how actual your MVP pattern is implemented.

Example:

interface BaseContract {
        interface BaseView {
            //Methods for View
            void onDoSomething();
        }

        interface BasePresenter {
            void doSomething();

        }
    }

    class BaseMainPresenter implements BaseContract.BasePresenter {
        BaseContract.BaseView view;

        BaseMainPresenter(BaseContract.BaseView view) {
            this.view = view;
        }

        @Override
        public void doSomething() {
            if (view != null)
                view.onDoSomething();
        }
    }

    class DemoClass implements BaseContract.BaseView {

        //Create object of Presenter 

        /****
         * Example :
         * BaseMainPresenter baseMainPresenter = new BaseMainPresenter(this);
         */
        @Override
        public void onDoSomething() {
            //Deal with Context here.
        }
    }

Refer below link for sample Actual implementation with scenario & learn more about Clean Architechure : https://github.com/android10/Android-CleanArchitecture

0

Here on github https://github.com/saksham24/Android-Firebase-Mvp-Mvc-Mvvm-chat i made a repo containing 3 applications with same functionality but written in 3 different android patterns(Mvc, Mvp, Mvvm)

Understanding three different pattern is quite easy if we get a simple good example on them so i made a repo to contribute my knowledge to this developer community. Also the repository is written using proper java guidelines and conventions(including naming and packages, modules) so people looking for such project can also view this repository.

now

  1. if you want to know the difference between android Mvp,Mvc, MvvM see this explanation by realm https://academy.realm.io/posts/eric-maxwell-mvc-mvp-and-mvvm-on-android/

  2. if you want to compare three pattern see this wonder full blog https://thinkmobiles.com/blog/mvp-vs-mvvm-android-patterns/

saksham
  • 3,173
  • 1
  • 21
  • 23
0

Now I read that android uses the MVP design pattern and try to understand this. The way I understand the MVP principle (in the android context) is that:

  • the Model layer are the logic classes

  • the presenter are the classes that are "linked" to an XML (the activities)

  • the passive view are the XML's.

Is this correct?

Not fully: for the Model layer it is true, but for the Presenter it is not. The Presenter is not linked to XML although it has reference to the View through its constructor. The View is the Activity/Fragment in android.

You might want to check here for a sample MVP app for android.

Community
  • 1
  • 1
Ali Nem
  • 5,252
  • 1
  • 42
  • 41