The previous article learned about the MVC framework pattern used in Android, not knowing what is the MVC framework pattern of the pro-poke here framework pattern MVC used in Android. In fact, the Google Android development team is to encourage developers to use the MVC Framework model development projects, we usually write code is more or less in the use of MVC framework pattern development projects, such as Google's own exit Volley network request framework is to follow the MVC framework. We can understand that the volley framework is a model of MVC, which is a piece of network data processing that does not need to be associated with the view views. Also conforms to view and model separation. You might think that the MVC framework is good enough to fit any project development, but from the previous blog you will find that controller controllers and view views are shown in a class activity, While the activity in Android is the role of controller controllers, if the interface is cumbersome and the view is complex, then we have to add more view display operations in the activity, which naturally increases the activity's code volume, It also leads to too much task and logic processing for the activity and unclear responsibilities. In this article we will introduce another framework model MVP.
Mvp
MPV has evolved from the classic MVC model, and its basic ideas are interlinked. where m is model models, providing business data; P is similar to the role of C in MVC, which is the presenter controller that performs logic processing. V is the view view that displays the data. There is a big difference between MVP and MVC: In MVP, view does not use model directly, communication between them is done through presenter (Controller in MVC), all interactions occur inside presenter, In MVC, view reads data from the direct model rather than through the Controller.
MVC Framework Diagram
MVP Frame Chart
The most important difference between MVC and MVP is the relationship between model and view, as shown in the frame chart above. In the MVC framework, view is able to directly read data from model models, and changes in model data are notified that the view data display changes accordingly. There is no link between model and view in the MVP, which is two completely independent modules, and when data changes occur in model models, the corresponding UI changes occur through presenter notification of the View view. Therefore, personally think: MVP is the true view and the model is completely separated, that is, model models for business data processing and view view display has no association.
MVP for Android
In the Andorid project, we are accustomed to use activity as a controller in MVC to achieve model and view separation, but in the MVP framework mode, activity is usually used as the view layer, Because the activity and the view view display are closely related in the MVC framework mode, the activity contains a lot of view display code, if the boss said the need to modify the view display, then do you feel the need to modify the activity of the large number of code? This will destroy the control logic in the activity and lead to too much responsibility in the activity. According to the principle of single responsibility, activity mainly plays the role of user interaction, that is, receiving user input, displaying the request result. Therefore, the role of activity can be mitigated by the MVP framework model. See how the Android project is implemented!
Also take one of the weather forecast small items to give examples:
Model Models
As with the MVC framework pattern, model models handle data code unchanged
/** * Created by XJP 2015-6-7 * Weather Model interface */ Public interface weathermodel { voidLoadweather (String Cityno, Onweatherlistener listener);} ........./** * Created by XJP on 2015/6/7. * Weather Model Implementation */ Public class Weathermodelimpl implements Weathermodel { @Override Public void Loadweather(String Cityno,FinalOnweatherlistener listener) {/ * Data-tier operation * /Volleyrequest.newinstance (). Newgsonrequest ("http://www.weather.com.cn/data/sk/"+ Cityno +". html", Weather.class,NewResponse.listener<weather> () {@Override Public void Onresponse(Weather Weather) {if(Weather! =NULL) {listener.onsuccess (weather); }Else{Listener.onerror (); } } },NewResponse.errorlistener () {@Override Public void Onerrorresponse(Volleyerror error) {Listener.onerror (); } }); }}
The data processed by model models is returned to the presenter controller via the Onweatherlistener interface callback.
Presenter Controller
/** * Created by XJP on 2015/6/7. * Weather Presenter Interface */ Public interface weatherpresenter { /** * Get the weather logic * / voidGetWeather (String cityno);} ........../** * Created by XJP on 2015/6/7. * In the presenter layer implementation, to the model layer callback, change the state of the view layer, to ensure that the model layer does not directly manipulate the view layer * / Public interface onweatherlistener { /** * Callback when successful * * @param Weather * * voidOnsuccess (Weather Weather);/** * Callback on failure, simple handling, nothing to do * * voidOnError ();} ......... PackageOrg.rocko.demos.mvp.presenter.impl;ImportOrg.rocko.demos.mvp.model.WeatherModel;ImportOrg.rocko.demos.mvp.model.entity.Weather;ImportOrg.rocko.demos.mvp.model.impl.WeatherModelImpl;ImportOrg.rocko.demos.mvp.presenter.OnWeatherListener;ImportOrg.rocko.demos.mvp.presenter.WeatherPresenter;ImportOrg.rocko.demos.mvp.ui.view.WeatherView;/** * Created by XJP on 2015/6/7. * Weather Presenter Implementation */ Public class Weatherpresenterimpl implements Weatherpresenter, Onweatherlistener { /*presenter as the middle tier, holding the view and model references */ PrivateWeatherview Weatherview;PrivateWeathermodel Weathermodel; Public Weatherpresenterimpl(Weatherview Weatherview) { This. Weatherview = Weatherview; Weathermodel =NewWeathermodelimpl (); }@Override Public void GetWeather(String Cityno) {weatherview.showloading (); Weathermodel.loadweather (Cityno, This); }@Override Public void onsuccess(Weather Weather) {weatherview.hideloading (); Weatherview.setweatherinfo (weather); }@Override Public void OnError() {weatherview.hideloading (); Weatherview.showerror (); }}
From the code we can see that the presenter Controller holds both Weathermodel and Weatherview objects and implements the Onweatherlistener interface to retrieve the model data, so Weatherpresenterimpl sends a data request to Weathermodel and then obtains the request result through the Onweatherlistener interface. The data is displayed in the view view of the activity as the result of the interface Weatherview. In order to completely separate the model and view, imagine that in this case, if you need to modify the model is not affected by the view code changes, in the same vein, when modifying the view layer, there is no need to modify the model layer. The equivalent model and view do not know each other's existence, is through the intermediary controller presenter to communicate.
View views
First define a View view display interface Weatherview
/** * Created by xjp on 2015/6/7. */publicinterface WeatherView { void showLoading(); void hideLoading(); void showError(); void setWeatherInfo(Weather weather);}
Then implement the activity to implement the Weatherview interface
/** * Weather interface * * Public class weatheractivity extends baseactivity implements Weatherview, View. Onclicklistener { PrivateDialog Loadingdialog;PrivateEditText Citynoinput;PrivateTextView City;PrivateTextView Cityno;PrivateTextView temp;PrivateTextView WD;PrivateTextView ws;PrivateTextView SD;PrivateTextView WSE;PrivateTextView time;PrivateTextView NJD;PrivateWeatherpresenter Weatherpresenter;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Init (); }Private void Init() {citynoinput = Findview (r.id.et_city_no); City = Findview (r.id.tv_city); Cityno = Findview (r.id.tv_city_no); temp = Findview (r.id.tv_temp); WD = Findview (R.ID.TV_WD); WS = Findview (R.ID.TV_WS); SD = Findview (R.ID.TV_SD); WSE = Findview (R.id.tv_wse); Time = Findview (r.id.tv_time); NJD = Findview (R.ID.TV_NJD); Findview (R.id.btn_go). Setonclicklistener ( This); Weatherpresenter =NewWeatherpresenterimpl ( This);//Incoming WeatherviewLoadingdialog =NewProgressDialog ( This); Loadingdialog.settitle ("Load the weather ..."); }@Override Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.btn_go:weatherpresenter.getweather (Citynoinput.gettext (). toString (). Trim ()); Break; } }@Override Public void showloading() {loadingdialog.show (); }@Override Public void hideloading() {Loadingdialog.dismiss (); }@Override Public void ShowError() {//do somethingToast.maketext (Getapplicationcontext (),"Error", Toast.length_short). Show (); }@Override Public void Setweatherinfo(Weather Weather) {Weatherinfo info = weather.getweatherinfo (); City.settext (Info.getcity ()); Cityno.settext (Info.getcityid ()); Temp.settext (Info.gettemp ()); Wd.settext (INFO.GETWD ()); Ws.settext (Info.getws ()); Sd.settext (INFO.GETSD ()); Wse.settext (Info.getws ()); Time.settext (Info.gettemp ()); Njd.settext (INFO.GETNJD ()); }}
As a result, the activity is freed from the controller in MVC, which will focus on the role of view and user interaction. Each activity can implement the View view interface Weatherview According to its own display view.
Summarize
- The MVP framework model completely separates the model and view views, thus allowing the coupling of the code, using the MVP framework to write the project to achieve decoupling.
- The biggest difference between MVP and MVC is that the V in MVC can get data from M, while the MVP in M and V are completely separated, unaware of each other's existence, and presenter communicates V and m by means of interface communication.
- In Android the MVP framework activity serves as the View view layer and the MVC Framework mode is not the same as the activity acting as controller.
Source Address Download: Source Address
Frame mode MVP usage in Android