Android MVP development model (19th)

Source: Internet
Author: User

Android MVP development model (19th)

 

 

(1). Preface:

Today, our project continues to be updated. Today, we will mainly explain the MVP development model and specific instances.

 

(2). Introduction:

The Model ViewPresenter Model is an evolutionary version of the famous Model ViewController Model. Currently, it is becoming more and more important in Android application development. At the beginning, it seems that we have added a lot of class interface code to make it clearer.

The MVP mode can be separated from the display layer and the logic layer, so how the functional interfaces work and the function display can be separated. The MVP mode can ideally implement the same logical code with different Display Interfaces. However, MVP is not a structured mode. It is only responsible for the display layer and can be used in its own project structure at any time. (Not limited to Android project development)

Because MVP actually evolved from the MVC model, let's take a look at the famous MVC model:

  • View: corresponds to the layout File
  • Model: business logic and entity Model
  • Controller: Controller, which corresponds to Activity in Android

    The interaction diagram is as follows:

     

    Although Android Application Development follows the MVC development mode, let's take a closer look at the View layer and Activity, the data binding and event processing methods in the specific view layout file are all redundant in the Activity, so we often see that the Activity class does not move, but only nine hundred rows are missing, there are thousands or even thousands of rows. So what about the upgraded MVP model? The MVP mode will introduce the Presenter layer. This Model is complex for interaction between the View layer and the Model layer. The MVP mode corresponds to the following:

    • View: View is usually implemented by Activity. It contains a Presenter reference. View only needs to be called every time an interface is called (for example, after a button is clicked) call the Presenter method.
    • Model: business logic and entity Model
    • Presenter: serves as a bridge between views and models. After retrieving data from the Model layer, it returns the data to the View layer, but unlike the MVC structure, it also determines the interaction with the View layer.

      The Data Interaction diagram is as follows:

      Observe the interaction diagram of the two modes above. Is the MVP mode clearer and simpler!

      (3) differences between MVC and MVP:

      Let's take a look at the two comparisons in the lower part of the timeline to see the specific differences:

      We can see that in MVP mode, the interaction between View and Model is completed through Presenter, so that unified management and logic are clearer.

      (4) MVP mode example:

      . Specific functional requirements: we use the MVP mode to implement user logon.

      4. Example:

      4. The project code framework is as follows:

      4. Code implementation:

      4.4.1.Model layer: Bean class (Entity) and PersonBean class. Then, there are logon methods in the business logic class, And the callback interface in the successful logon status is passed in as follows:

       

      Packagecom. chinaztt. fda. entity;/*** current class annotation: User Information entity class * Project name: FastDev4Android * package name: com. chinaztt. fda. entity * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: Jiangsu Zhongtian Technology Software Technology Co., Ltd. */public classPersonBean {private String username; private String password; public PersonBean () {} public PersonBean (String username, Stringpassword) {this. username = username; this. password = password;} public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} @ Override public String toString () {return PersonBean {+ username = '+ username + ''' +, password = '+ password + ''' + '}';}}
      public interfaceIPersonBiz {    void login(String username,Stringpassword,LoginRequestCallBack valueCallBack);}
      Packagecom. chinaztt. fda. biz. imp; importcom. chinaztt. fda. biz. IPersonBiz; importcom. chinaztt. fda. biz. loginRequestCallBack; importcom. chinaztt. fda. entity. personBean; importcom. chinaztt. fda. utils. log;/*** current class comment: User-related business logic implementation class * Project name: FastDev4Android * package name: com. chinaztt. fda. biz. imp * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public classPersonBizImp implements IPersonBiz {private static final String TAG = PersonBizImp; @ Override public void login (final String username, final String password, final LoginRequestCallBack valueCallBack) {Log. d (TAG, username: + username +, password: + password); new Thread (new Runnable () {@ Override public void run () {try {Thread. sleep (4500);} catch (InterruptedExceptione) {e. printStackTrace () ;}// start to log on. Here, we should request the server for data verification if (username. equals (jiangqq) & password. equals (12345) {valueCallBack. loginSuccess (new PersonBean (username, password);} else {valueCallBack. loginFailed ();}}}). start ();}}
      Packagecom. chinaztt. fda. biz; importcom. chinaztt. fda. entity. personBean;/*** current class annotation: callback of login request result * Project name: FastDev4Android * package name: com. chinaztt. fda. biz * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public interfaceLoginRequestCallBack {// callback Method for Logon success void loginSuccess (PersonBean personBean); // callback Method for Logon Failure void loginFailed ();}

       

      4.4.2.View layer: This layer interacts with the View through Presenter. Here, an interface ILoginView needs to be defined:

       

      Packagecom. chinaztt. fda. ui. view; importcom. chinaztt. fda. entity. personBean;/*** current class annotation: Operation Interface related to logon page * Project name: FastDev4Android * package name: com. chinaztt. fda. ui. view * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public interfaceILoginView {// obtain the username String getUserName (); // obtain the password String getPassword (); void showSuccessInfo (PersonBeanpersonBean ); void showFailedInfo ();}

       

      With the above interface, we need to write our implementation class Activity, which is very simple.

       

      Packagecom. chinaztt. fda. test; importandroid. OS. bundle; importandroid. view. view; importandroid. widget. button; importandroid. widget. editText; importcom. chinaztt. fda. entity. personBean; importcom. chinaztt. fda. presenter. loginPresenter; importcom. chinaztt. fda. ui. r; importcom. chinaztt. fda. ui. base. baseActivity; importcom. chinaztt. fda. ui. view. ILoginView; importcom. chinaztt. fda. utils. log; importorg. androidannotations. annotations. EActivity;/*** Note of the current class: MVP development mode instance * Project name: FastDev4Android * package name: com. chinaztt. fda. author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */@ EActivitypublic classMVPTestActivity extends BaseActivity implements ILoginView {private static final String TAG = MVPTestActivity; private EditText ed_username; private EditText ed_password; private Button secret; private parameter secret; @ Override protected void onCreate (BundlesavedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. mvp_test_layout); ed_username = (EditText) this. findViewById (R. id. ed_username); ed_password = (EditText) this. findViewById (R. id. ed_password); btn_login = (Button) this. findViewById (R. id. btn_login); mLoginPresenter = newLoginPresenter (this); btn_login.setOnClickListener (newView. onClickListener () {@ Override public void onClick (View v) {mLoginPresenter. loginSystem () ;}}) ;}/ *** returns the username information * @ return */@ Override public String getUserName () {returned_username.getText (). toString (). trim ();}/*** returns the user password information * @ return */@ Override public String getPassword () {returned_password.getText (). toString (). trim ();}/*** callback for Successful Logon * @ param personBean */@ Override public void showSuccessInfo (PersonBeanpersonBean) {Log. d (TAG, showSuccessInfo: + personBean. toString (); showToastMsgShort (LOGIN successful: + personBean. toString ();}/*** logon Failure callback */@ Override public void showFailedInfo () {Log. d (TAG, showFailedInfo ...); showToastMsgShort (logon failed ...);}}

       

      At last, there is still one less interaction bridge Presenter:

      4.4.3.Presenter layer: serves as an interactive bridge between the Model and View. In this example, perform the logon operation, log on to the Model business, and return the logon result to the View layer, this is simple:

       

      Packagecom. chinaztt. fda. presenter; importandroid. OS. handler; importcom. chinaztt. fda. biz. IPersonBiz; importcom. chinaztt. fda. biz. loginRequestCallBack; importcom. chinaztt. fda. biz. imp. personBizImp; importcom. chinaztt. fda. entity. personBean; importcom. chinaztt. fda. ui. view. ILoginView; importcom. chinaztt. fda. utils. log;/*** current class annotation: responsible for completing the interaction between the logon interface View and Model (IPersonBiz) * Project name: FastDev4Android * package name: com. chinaztt. fda. presenter * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public classLoginPresenter {private static final String TAG = LoginPresenter; private ILoginView mLoginView; private IPersonBiz mPersonBiz; private Handler mHandler = new Handler (); public LoginPresenter (ILoginView view) {mLoginView = view; mPersonBiz = new PersonBizImp ();} public void loginSystem () {mPersonBiz. login (mLoginView. getUserName (), mLoginView. getPassword (), newLoginRequestCallBack () {/*** logon successful * @ param personBean */@ Override public void loginSuccess (finalPersonBean personBean) {Log. d (TAG, logon successful: + personBean. toString (); mHandler. post (new Runnable () {@ Override public void run () {mLoginView. showSuccessInfo (personBean) ;}}) ;}/ *** Logon Failed */@ Override public void loginFailed () {Log. d (TAG, Logon Failed ...); mHandler. post (new Runnable () {@ Override public void run () {mLoginView. showFailedInfo ();;}});}});}}

       

      At this point, our MVP mode example is basically completed. Let's take a look at the above demonstration and we will be OK.

       

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.