MVP Design for Android: How to adopt to my application?

我怕爱的太早我们不能终老 提交于 2019-12-20 06:00:46

问题


I am writing an application that will have a JSON file which contains data that should be parsed and saved into ItemModel POJOs.

Let's assume for now I have a simple Activity that displays nothing at all - all I want my application to do is on startup parse the JSON and create the model objects (I'd like to figure out the basic architecture of these pieces before moving onto bigger/better things). I'd like to unit test this code before making any UI components to ensure my model classes are working OK.

So, the ItemModel is obviously a model object.

Where does the JSON parsing fall in terms of MVP? The library to parse the data (Gson, Jackson, or something else?) will surely require an Android Context to achieve this, so should I be parsing that information in the Activity? If yes, now the view knows about Model classes, which breaks MVP.

Also if I wanted to them persist the ItemModel/JSON data in a database, where would that be done? The database should technically be in the model - but again that requires a Context in order to work correctly...

Any thoughts? Thanks!


回答1:


There is no exact/correct definition of implementing MVP in Android

Here's a great article on MVP

I'd do MVP as follow.

  1. Model - POJO's, parsing, Storing (SQLlite) and retrieving data (http). Obviously I'd divide the POJO's, parsing and DB logic into sub folders - but this all falls into Model for me.
  2. View - Activity, Fragment, Adapters - Activities & Fragment hold reference to a Presenter that gives them data to display. How this data/messages are displayed, look + feel etc. is dealt with in the View.
  3. Presenter - The Middle man, provides the logic to inputs i.e. Button Clicks, retrieval of data, validation of inputs & then passes the result back to the View (Activity or Fragment)

Here's a simplified diagram of MVP



来源:https://stackoverflow.com/questions/41176007/mvp-design-for-android-how-to-adopt-to-my-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!