The first knowledge of the Android MVP mode

Source: Internet
Author: User

What is MVP? Perhaps a lot more people know about the MVC model (model View Controller), but the Most different point between M VP and MVC is that M and V are not directly

The correlation is also the model and the view does not have a direct relationship between the two are spaced between the presenter layer. Personal feeling this is a great design that allows the code to be fully decoupled.

So we're still not talking about what the MVP is ~ ~ ~ Don't hurry, I will use the simplest way to narrate, so good understanding ~

    

M (Model): Provides data for the UI layer, or holds data from the UI layer;

V (View): Simple data display, in response to the user's operation and are forwarded to the presenter to do specific processing;

P (Presenter): Logic control layer, take data from model, operation and transformation, and finally show with view, and deal with the user event that view passes over, and do the processing;

Do you feel at a glance? Many of the first learning design patterns will have a question, that is, the design pattern of this thing sounds like a very tall, then under what circumstances should we use MVP this design mode? In fact:
    • Basically all can, even if only one login function, also can use MVP, we beginner MVP affirmation goal is to learn, so to master knowledge, wait until understanding, large projects can be used;
    • MVP It is a methodological thing, there is no fixed way of implementation, as long as it can reflect its methods can be regarded as MVP.
Of course, we said that the feeling of a little flashy feeling, after all, it is said that no one knows how to use MVP, or a little bit of rules will be better let usto understand: 
    • Model and view cannot communicate directly, only through presenter
    • Presenter a role similar to a middleman in coordination and scheduling
    • Model and view are interfaces, presenter holds a model interface and a view interface
    • Both model and view should be passive, and everything is dominated by presenter.
    • Model should encapsulate the interaction with the business logic layer, in other words presenter and view should not know the business logic layer
    • The logic of the view should be as simple as possible and should not have a state. When an event occurs, call presenter to process it, and do not pass arguments, and then call the view method to get the presenter processing.
It is easy to see that the key is still on the presenter above, it can be said that presenter is the core of our entire MVP design model, then theoretically said so much, how to implement it concretely? Implementation in Android

    

    MVP is a methodological thing, that is, there is no fixed specific form of implementation, as long as the view can be removed from the model, the logic is placed in the presenter, then can calculate

to be MVP, some specific guiding principles of Practice:

    • View is an interface that is responsible for passively displaying the processed data.
    • Model is also an interface that is responsible for acquiring data and storing data
    • The View call presenter handling user events is also an interface called event delegate
    • Presenter holds the view interface and the model interface

Next we use a login example to illustrate:

Interface for login view: 

 Public Interface Iloginview {    void  clearedittext ();     void showprogress ();     void hideprogress ();     void setusernameerror ();     void setpassworderror ();    String GetUserName ();    String GetPassword ();     void loginsuccess ();}

Login Presenter Interface:
 Public Interface iloginpresenter {    void  Dologin (string Username, string password);     void Clear ();     void OnDestroy ();}
Implement the Presenter interface:
 Public classLoginpresenterImplementsIloginpresenter {PrivateIloginview Mloginview; PrivateUser Muser;  PublicLoginpresenter (Iloginview loginView) { This. Mloginview =LoginView;    Inituser (); }    Private voidInituser () {Muser=NewUser (Mloginview.getusername (), Mloginview.getpassword ()); } @Override Public voidDologin (string Username, string password) {mloginview.showprogress (); NewHandler (). postdelayed (NewRunnable () {@Override Public voidrun () {mloginview.hideprogress (); intCode =muser.checkuservalidity (Mloginview.getusername (), Mloginview.getpassword ()); if(Code = =-1) {mloginview.setpassworderror (); } Else if(Code = = 0) {mloginview.loginsuccess (); }            }        }, 2000); } @Override Public voidClear () {mloginview.clearedittext (); } @Override Public voidOnDestroy () {Mloginview=NULL; }}

Define model:

 Public classUser {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicUser (string Username, string password) { This. Username =username;  This. Password =password; }     Public intcheckuservalidity (string Username, string password) {if(Username = =NULL|| Password = =NULL||Username.isempty ()||Password.isempty ()) {            return-1; }        return0; }}

Implement view in activity:

 Public classLoginactivityextendsappcompatactivityImplementsIloginview, View.onclicklistener {PrivateIloginpresenter Mloginpresenter;    @Bind (r.id.et_username) EditText etusername;    @Bind (R.id.et_passwrod) EditText Etpasswrod;    @Bind (r.id.bt_enter) Button btenter;    @Bind (r.id.bt_clear) Button btclear;    @Bind (r.id.progress) ProgressBar progress; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Butterknife.bind ( This); Mloginpresenter=NewLoginpresenter ( This); Btenter.setonclicklistener ( This); Btclear.setonclicklistener ( This); } @Override Public voidClearedittext () {Etpasswrod.settext (""); Etusername.settext (""); } @Override Public voidshowprogress () {progress.setvisibility (view.visible); } @Override Public voidhideprogress () {progress.setvisibility (view.gone); } @Override Public voidSetusernameerror () {Etusername.seterror ("Username Error"); } @Override Public voidSetpassworderror () {Etpasswrod.seterror ("Password Error"); } @Override PublicString GetUserName () {returnEtusername.gettext (). toString (); } @Override PublicString GetPassword () {returnEtpasswrod.gettext (). toString (); } @Override Public voidloginsuccess () {//Start Act MainToast.maketext ( This, "Login Success", Toast.length_short);    Finish (); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.bt_clear:mloginpresenter.clear ();  Break;  CaseR.id.bt_enter:mloginpresenter.dologin (Etusername.gettext (). toString (), ETPASSW                Rod.gettext (). toString ());  Break; }} @Overrideprotected voidOnDestroy () {Mloginpresenter.ondestroy (); Super. OnDestroy (); }}
This is not a more profound understanding of the MVP, if you do not understand the best is to do it yourself, you will understand faster oh ~~~~~

The first knowledge of the Android MVP mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.