简体   繁体   中英

MVP Implementation in Android

I am looking into MVP architecture Implementation in android. I found too many ways(mention end of the question) to implement it in the android studio, but Still, I am confused.

Can someone help me to find the right answer of below questions.

  • What would be directory structure of Application in MVP?
  • Activity should be a Presenter or View?

Way-1

Way-2

What would be directory structure of Application in MVP?

  • There is no predefined structure for that. What makes your code readable or what structure you are following, you can use same for MVP also.

Activity should be a Presenter or View?

  • View is the UI layer which displays the data and notifies the Presenter about user actions. So Activity will always be a view.

If you are looking for a good example of MVP implementation, there is one GitHub Repo for MVP developed by Android itself. Which you should look into.

Where

todo‑mvp

  • Demonstrates a basic Model‑View‑Presenter (MVP) architecture and provides a foundation on which the other samples are built. This sample also acts as a reference point for comparing and contrasting the other samples in this project.

todo‑mvp‑clean

  • Uses concepts from Clean Architecture.

todo‑mvp‑dagger

  • Uses Dagger 2 to add support for dependency injection.

todo‑mvp‑rxjava

  • Uses RxJava 2 to implement concurrency, and abstract the data layer.

todo‑mvvm‑databinding

  • Based on the todo-databinding sample, this version incorporates the Model‑View‑ViewModel pattern.

todo‑mvvm‑live

  • Uses ViewModels and LiveData from Architecture Components and the Data Binding library with an MVVM architecture.

Though there are lot of implementations for an MVP architecture, all of them share a basic idea (or they should at least), which is separating business logic from your views (activities, fragments, dialogs). Why is that? Well, for two reasons mainly:

  1. separation of concerns
  2. Testability: your business logic is able to be tested if there is no android components involved.

About your questions:

What would be directory structure of Application in MVP?

There is no rule about that except that your MVP components should be identified. Here you have an article where I started with a package structure but then I found other more convenient.

Activity should be a Presenter or View?

Your activity (or fragment or whatever components in charge of showing view components) should be the one that implements your view.

My advice is that you should check multiples examples and see their advantages and disadvantages of each one, and try to define your own architecture from those which you will feel more comfortable with.

You can use either Activity or Fragment for View layer. This is because showing UI elements in android needs Context .

For the Presenter layer, you must make sure not to pass the Context to the Presenter via constructor or setter. If you needed Context in your Presenter for tasks other than showing the UI, such as writing to SharedPreferences , you can get it from your View (which is either Activity or Fragment ). In this way, if the View gets destroyed or becomes null , there would no standalone null Context in the Presenter to cause leak issues.

If you want to know more about the MVP structure, I have written a very handy MVP library for android and explained its use in a sample app here .

MVP android示例示例应用程序 MVP Android Example用来解释如何在我们的Android应用程序中使用此模式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM