Android Advanced Road (2) - Detailed MVP

### MVP Profile> MVP full name: Model-View-Presenter; MVP is from the classic MVC pattern evolved, they have in common is the basic idea of ​​[local] (https://baike.baidu.com/item/% E5% 9C% B0% E6% 96% B9 / 2262175): Controller / Presenter responsible for logic processing, Model provides data, View is responsible for displaying. For MVC friends do not understand you can check this article: [Android Advanced Road (1) - Detailed MVC] (https://www.jianshu.com/p/285f6a8d971f) ** Android in MVP: ** - M layers: suitable for some business logic, such as database access operations, network operations, complex algorithms, and so time-consuming task in the process model layer. And the like MVC - V layer: corresponding Activity, responsible for interaction with the page rendering xml - P layer: View is responsible for interaction between the Model [] (https://img2018.cnblogs.com/blog/1312938/201909/1312938! -20190901084219353-399431689.png) ### MVP why should we think about the last article, we talked about why use MVC, when we summed up the points: - code reuse - low coupling - easy to maintain higher However, when we write MVC project, but also found some problems: 1. for Android in the Activity or Fragment, we can not clearly distinguish it View or Controller, there are both interactive page rendering, which led to the activity and fragment was "huge" 2. View and Model are really isolated, but the correlation is too strong, which led to strong activity and model of contact, so we just changed a little bit of code, model, view, activity will change, maintenance costs are too high, before the View and the Model coupling is too high. The biggest change is the MVP View and Model achieve complete isolation. The operation is intended to View P layer, the P received calls Model layer to achieve a specific logic, the logic implementation will then inform the P, then P by View View interface to the callback. Even if you change the V and M, it is not affected, low coupling. ### MVP DEMO to log module, for example, to achieve defined View Interface MVP ** 1 - ILoginView ** `` `/ ** * Get view layer dialog * * @return retuen * / Dialog getLoadDialog ();. / *** * close view layer dialog * / void cancelLoadDialog (); / ** * get the phone number parameters * * @return username * / String getPhone (); / ** * Gets password * * @return password * / String getCode (); / ** * pop-up message * * @param msg msg * / void showMsg (String msg); `` `View the interface we all know, right? What you want to tell the intent of P. activity implement this interface. P and call methods: `` `@Override public void cancelLoadDialog () {if (dialog = null && dialog.isShowing ()!) {Dialog.dismiss ();}} @Override public String getPhone () {return edPhone.getText ( ) .toString (); onLoadDatasListener); } ``` ModelImpl : ``` @Override public void login(String phone, String code, final OnLoadDatasListener onLoadDatasListener) {// fill in the operating logic of the specific callback onLoadDatasListener} `` `model implementation class will be notified after completion logic to P, defined P:` `` / ** * login * / public void login () {if ( mView == null) return; if (TextUtils.isEmpty (mView.getPhone ()) || TextUtils.isEmpty (mView.getCode ())) {mView.showMsg ( "phone number or password is not blank"); return; } mView.getLoadDialog () show ();. loginModel.login (mView.getPhone (), mView.getCode (), new OnLoadDatasListener () {@Override public void onSuccess (CurrencyBean.DataBean dataBean) {Gson gson = new Gson (); mView.cancelLoadDialog (); Log.e ( "qzs ----", gson.toJson (dataBean) + "" ); mView.loginSuccess ();} @Override public void onFailure (String error) {mView.cancelLoadDialog (); mView.loginFail ();}});} `` `P is returned to by View View Interface is also a further MVP disadvantages: - increase in code complexity - the added difficulty - If rendering a particular view of a lot of (activity), it will cause the Presenter view too closely associated with, once the view needs to be changed, you also need to change the Presenter, not as expected, the increase and reduce the coupling reusability. We can focus on my micro-channel public number: "Qin Shuai child" a quality, public attitudes numbers! ! [Public number] (https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190901084219519-1281896463.jpg)

Guess you like

Origin www.cnblogs.com/qinzishuai/p/11441365.html