Introduction to Android Development Learning--MVP mode

Source: Internet
Author: User

1, the model and the view completely separate, we can modify the view without affecting the model
2. Models can be used more efficiently because all interactions occur in one place--presenter internal
3, we can use a presenter for multiple views without changing the logic of presenter. This feature is very useful because view changes are always more frequent than models.
4. If we put logic in presenter, then we can test the logic out of the user interface (unit test)

The following example is a small case:

Project structure:

First, the bean layer:

User.java

 Public classUser {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     PublicString GetPassword () {returnpassword; }     Public voidSetusername (String username) { This. Username =username; }     Public voidSetPassword (String password) { This. Password =password; }}

Mainactivity.java

 Public classMainactivityextendsAppcompatactivityImplements iuserloginview{PrivateEditText Musername; PrivateEditText Mpasssword; PrivateButton Mlogin; PrivateButton McLear; PrivateProgressBar MPB; PrivateUserloginpresenter Muserloginpresenter =New Userloginpresenter ( This); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {musername=(EditText) Findviewbyid (r.id.username); Mpasssword=(EditText) Findviewbyid (R.id.password); Mlogin=(Button) Findviewbyid (R.id.login); McLear=(Button) Findviewbyid (r.id.clear); MPB=(ProgressBar) Findviewbyid (R.ID.PB); Mlogin.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Muserloginpresenter.login ();        }        }); Mclear.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {muserloginpresenter.clear ();    }        }); } @Override PublicString GetUserName () {returnMusername.gettext (). toString (); } @Override PublicString GetPassword () {returnMpasssword.gettext (). toString (); } @Override Public voidClearusername () {Musername.settext (""); } @Override Public voidClearpassword () {Mpasssword.settext (""); } @Override Public voidshowloading () {mpb.setvisibility (view.visible); } @Override Public voidhideloading () {mpb.setvisibility (view.gone); } @Override Public voidtomainactivity (user user) {Toast.maketext ( This, User.getusername () + "login success, to Mainactivity", Toast.length_short). Show (); } @Override Public voidShowfailerror () {Toast.maketext ( This,                "Login Failed", Toast.length_short). Show (); }}
Iuserloginview. java
 Public InterfaceIuserloginview {//purpose of the OperationString GetUserName ();    String GetPassword (); voidClearusername (); voidClearpassword (); //Friendly Interaction    voidshowloading (); voidhideloading (); //results of the operation, corresponding feedback    voidtomainactivity (user user); voidshowfailerror ();}
userloginpresenter. java
//model and view Bridge login clear Public classUserloginpresenter {Private Iuserloginview Userloginview; Private iuserbiz userbiz; PrivateHandler Mhandler =NewHandler ();  PublicUserloginpresenter (Iuserloginview userloginview) { This. Userloginview =Userloginview;  This. userbiz =Newuserbiz (); }     Public voidLogin () {userloginview.showloading (); Userbiz.login (Userloginview.getusername (), Userloginview.getpassword (),New Onloginlistener () {@Override Public voidLoginsuccess (FinalUser User) {                //needs to be executed on the UI threadMhandler.post (NewRunnable () {@Override Public voidrun () {userloginview.tomainactivity (user);                    Userloginview.hideloading ();            }                }); } @Override Public voidLoginfail () {//needs to be executed on the UI threadMhandler.post (NewRunnable () {@Override Public voidrun () {userloginview.showfailerror ();                    Userloginview.hideloading ();            }                });    }        }); }     Public voidClear () {userloginview.clearusername ();    Userloginview.clearpassword (); }}
iuserbiz. java
 Public Interface iuserbiz {    publicvoid  login (String username,string password, Onloginlistener loginlistener);}
userbiz. java
 Public classUserbizImplements iuserbiz {@Override Public voidLoginFinalString username,FinalString Password,FinalOnloginlistener Loginlistener) {        NewThread () {@Override Public voidrun () {Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }                if("Name". Equals (username) && "pwd". Equals (password)) {User User=NewUser ();                    User.setusername (username);                    User.setpassword (password);                loginlistener.loginsuccess (user); }Else{loginlistener.loginfail ();    }}}.start (); }}

Onloginlistener.java

 Public Interface Onloginlistener {    void  loginsuccess (user user);     void loginfail ();}

Introduction to Android Development Learning--MVP mode

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.