Preface
On the architecture of the article, bloggers have long wanted to write, although recently more popular MVVM, but MVP and MVC is not outdated, the most important thing is to choose the right architecture according to the business. Of course, now write a lot of MVP articles, there are a lot of good articles, but most of the reading is still confused, with the least text expression is my usual style (here a small outfit forcing a bit), so it is more reliable to summarize.
1. Review MVC
Before we talk about the MVP, we need to look back. MVC,MVC (Model-view-controller, model-View-Controller) mode is a software design pattern that appeared in the 80 's Smalltalk-80, and it has been widely used, with a kind of business logic, data, The interface shows a separate approach to organizing code, which requires no rewriting of business logic while improving and personalizing the interface and user interaction.
the MVC of Android
The MVC framework can also be used in the interface section of Android, and the role definitions of MVC are:
-
Model Layer
The data structures and related classes that we set up for the business model, can be understood as Model,model is not related to view, but business-relevant.
-
View Layer (views)
Generally use XML files or Java code to describe the interface, you can also use javascript+html and other methods as the view layer.
-
Control Layer (Controller)
Android's control layer is typically in acitvity, fragment, or other business classes that are controlled by them.
The MVC disadvantage of Android
In Android development, activity is not a controller in the standard MVC pattern, its primary responsibility is to load the application's layout and initialize the user interface, and accept and process requests from the user to respond. As the complexity of the interface and its logic increases, so does the responsibility of the activity class to become bloated.
2. What is MVP
MVP (Model View Presenter) is an evolutionary version of MVC, and the MVP's role definitions are:
-
Presenter
As a bridge between view and model, it retrieves data from the model layer and returns it to the view layer, making no coupling between view and model.
-
Model
It mainly provides data access function. Presenter need to store and retrieve data through the model layer.
-
View
Responsible for handling the display of user events and view sections. In Android, it could be an activity, a fragment class, or a view control.
In the MVP, presenter completely separates the model from the view, and the main program logic is implemented in presenter. Furthermore, presenter is not directly associated with a specific view, but interacts through a well-defined interface, allowing the presenter to remain unchanged when the view is changed. View should only have a simple Set/get method, the user input and Settings interface display content, in addition to this should not have more content, never allow direct access to model, this is a big difference with MVC.
3. Using the MVP
Here we give an example, through the Web to get the title and content of the article and display in the interface, access to network content and Android network programming (iii) Volley usage full parse the data used in this article is the same, JSON data format click here.
Access to network data is okhttpfinal, as shown in the package directory:
Implement Model
First we'll create the bean file, and here's some code:
Public classArticleinfo {PrivateString desc;PrivateString status;Privatelist<detail> detail =NewArraylist<detail> (); PublicList<articleinfo.detail>Getdetail() {returnDetail } Public void Setdetail(list<articleinfo.detail> detail) { This. detail = detail; } ... Omitted Public classDetail {PrivateString title;PrivateString Article_url;PrivateString my_abstract;PrivateString Article_type; PublicStringGetTitle() {returnTitle } Public void Settitle(String title) { This. title = title; }... Omitted
The next step is to get the model interface class for the article, which defines how to get the data:
publicinterface ArticleModel { void getArtcle(OnArticleListener onArticleListener);}
There is a callback listener interface that defines the various states of the network access callback:
publicinterface OnArticleListener { void onSuccess(ArticleInfo articleInfo); void onStart(); void onFailed(); void onFinish();}
Next we write the Articlemodel implementation class to get the data:
Public class Articlemodelimpl implements Articlemodel { @Override Public void getartcle(FinalOnarticlelistener onarticlelistener) {httprequest.post ("Http://api.1-blog.com/biz/bizserver/article/list.do",NewBasehttprequestcallback<articleinfo> () {@Override protected void onsuccess(Articleinfo articleinfo) {Super. OnStart (); Onarticlelistener.onsuccess (Articleinfo); }@Override Public void OnStart() {Super. OnStart (); Onarticlelistener.onstart (); }@Override Public void onfailure(intErrorCode, String msg) {Super. OnFailure (ErrorCode, msg); Onarticlelistener.onfailed (); }@Override Public void onfinish() {Super. OnFinish (); Onarticlelistener.onfinish (); } }); }}
Get the data through okhttpfinal and call your own defined callback function in the callback function.
Implement presenter
First define the Articlepresenter interface:
publicinterface ArticlePresenter { void getArticle();}
Implement the Articlepresenter interface:
Public class Articlepresenterimpl implements Articlepresenter, Onarticlelistener { PrivateArticleview Marticleview;PrivateArticlemodel Marticlemodel; Public Articlepresenterimpl(Articleview Marticleview) { This. Marticleview = Marticleview; Marticlemodel =NewArticlemodelimpl (); }@Override Public void getArticle() {marticlemodel.getartcle ( This); }@Override Public void onsuccess(Articleinfo articleinfo) {Marticleview.setarticleinfo (articleinfo); }@Override Public void OnStart() {marticleview.showloading (); }@Override Public void onfailed() {marticleview.showerror (); }@Override Public void onfinish() {marticleview.hideloading (); }}
It is obvious that the Articlepresenterimpl contains Articlemodel and Articleview instances (later), by implementing the Onarticlelistener interface and calling Articlemodel to fetch the data and callback it to itself, Finally, through the Articleview to interact with the activity, to change the interface. This time we should understand that presenter is the role of an intermediary who obtains and saves data through the model, and then updates the interface through the view. During this period, the view and model have no interaction by defining the interface. Finally, let's look at the implementation of the view layer:
Implement View
Articleview is used to define the interface interaction method:
publicinterface ArticleView { void setArticleInfo(ArticleInfo articleInfo); void showLoading(); void hideLoading(); void showError();}
We call Articlepresenterimpl in the activity:
Public class mainactivity extends baseactivity implements Articleview { PrivateButton bt_getarticle;PrivateTextView Tv_article_title;PrivateTextView tv_article_content;PrivateArticlepresenter Marticlepresenter;PrivateDialog Mdialog;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); }Private void Initview() {marticlepresenter=NewArticlepresenterimpl ( This); mdialog=NewProgressDialog ( This); Mdialog.settitle ("Get data in"); Bt_getarticle = Findview (r.id.bt_getarticle); Tv_article_title = Findview (R.id.tv_article_title); Tv_article_content = Findview (r.id.tv_article_content); Bt_getarticle.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {marticlepresenter.getarticle (); } }); }@Override Public void Setarticleinfo(Articleinfo articleinfo) {if(NULL!=articleinfo) {list<articleinfo.detail> List = Articleinfo.getdetail ();if(NULL!=list&&list.size () >1) Tv_article_title.settext (List.get (1). GetTitle ()); Tv_article_content.settext (List.get (1). Getmy_abstract ()); } }@Override Public void showloading() {mdialog.show (); }@Override Public void hideloading() {if(Mdialog.isshowing ()) {Mdialog.dismiss (); } }@Override Public void ShowError() {Toast.maketext (Getapplicationcontext (),"Network Error", Toast.length_short). Show (); }}
It is important to note that mainactivity implements the Articleview interface, which is used to receive the callback update interface, and it is clear that mainactivity does not do anything other than interface-agnostic.
advantages and disadvantages of 4.MVP
Advantages
- Reduce coupling to achieve a truly complete separation of model and view.
- Module responsibilities are clearly divided, the level is clear.
- Presenter can be reused, and a presenter can be used for multiple view without changing the logic of presenter (although the change in view does not affect the business logic).
- If we put logic in presenter, we can test these logic (unit tests) out of the user interface.
Disadvantages
- Additional code complexity and learning costs.
- If the presenter is too tightly tied to a particular view, the presenter needs to be changed once the view needs to be changed.
5. Summary
Well, the MVP example here, there are many ways to achieve the MVP, here I just talk about a most basic way, but the change is not one of them. A brief summary of the relationship between the MVP is: there is no connection between the view and model, view through the interface and presenter interaction, model is not active and presenter contact, passive waiting for presenter to call its interface, Presenter is contacted via interface and View/model.
GitHub Source Download
Resources:
MVP in Android
Android app Design Architecture: MVC,MVP,MVVM and architecture experience
An explanation of the MVP architecture in Android development
Talking about MVP in Android
Refactoring code using MVP mode
Is the MVP mode your lifeline?
Android MVP Details (top)
The pros and cons of MVP mode
Android MVP Instance Application
Android Architecture (i) MVP full resolution