问题
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.
- 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.
- 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. - 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
orFragment
)
Here's a simplified diagram of MVP
来源:https://stackoverflow.com/questions/41176007/mvp-design-for-android-how-to-adopt-to-my-application