Android MVP design model !, Androidmvp Design Model

Source: Internet
Author: User

Android MVP design model !, Androidmvp Design Model

Implementation principle:

MainActivity is used to update the UI and display the results of the business logic!

LoginPresenterCompl is used to process business logic

ILoginPresenter business processing class abstract interface

Interface abstracted by ILoginView activity

1. Why abstract the UI update method of the activity?

Because your project cannot have only one activity. If you want to use the MVP mode for each activity, you can abstract the common methods.

2. Why abstract the business processing class?

Similarly, the business processing logic of each activity is definitely different. We can use the java feature, that is, rewrite. Although the method names, parameters, and return types of each business class are the same, but we can process different logic in it.

For example, if A is processing watermelon in it, and B is processing pears in it, all types of fruit are returned!

3. Why the MVP mode?

When you have a large amount of code, if you do not use a good framework, the subsequent maintenance and search will become messy, and you do not know how to find them. If you encounter problems, you will not be able to analyze them as soon as possible, in short, a good APK should be built using a good framework in the early stage!

The first is the activity code:

Ublic class MainActivity extends Activity implements ILoginView {

Private Button button1;
Private Button button2;
Private EditText edit1;
Private EditText edit2;
Private ILoginPresenter presenterCompl;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
PresenterCompl = new LoginPresenterCompl (this );
Button1 = (Button) findViewById (R. id. button1 );
Button2 = (Button) findViewById (R. id. button2 );
Edit1 = (EditText) findViewById (R. id. edit1 );
Edit2 = (EditText) findViewById (R. id. edit2 );

Button1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
PresenterCompl. doLogin (edit1.getText (). toString (), edit2.getText (). toString ());
}
});
Button2.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
PresenterCompl. clear ();
}
});

}
@ Override
Public void onClearText (){
Edit1.setText ("");
Edit2.setText ("");
Toast. makeText (getApplicationContext (), "clear", 1). show ();
}

@ Override
Public void onLoginResult (Boolean result, int code ){
If (result ){
Toast. makeText (getApplicationContext (), "Logon successful", 1). show ();
} Else {
Toast. makeText (getApplicationContext (), "Logon Failed", 1). show ();
}
}

}

It implements the ILoginView interface, which defines two methods, one for clearing edittext data and the other for displaying the processing results of the logical task class, the results can be determined based on your company's specific business needs!

Public interface ILoginView {
Public void onClearText ();
Public void onLoginResult (Boolean result, int code );
}

Next, the logical task class LoginPresenterCompl

Public class LoginPresenterCompl implements ILoginPresenter {
Private ILoginView mView;
Private User user;
@ Override
Public void clear (){
MView. onClearText ();
}

@ Override
Public void doLogin (String name, String password ){
Boolean result = false;
Int code = 0;
If (name. equals (user. getName () & password. equals (user. getPassword ())){
Result = true;
Code = 1;
} Else {
Result = false;
Code = 0;
}
MView. onLoginResult (result, code );
}
Public LoginPresenterCompl (ILoginView view ){
This. mView = view;
User = new User ("000", "123456 ");
}
}

This class implements the ILoginPresenter interface, because there will certainly be more than one case for your project's logical tasks in the future, so we abstract the common methods.

You only need to implement this interface. The specific task logic depends on your company's needs. For example, we use login verification to obtain user input in the activity.

And then compare it with the data of the entity class (note that the entity class is written for convenience, the actual situation should be from the database

Or retrieve data from the network request and save the data to the object class. This is not specific here.) Finally, call the abstract UI update method in the activity,

Actually, the result is returned to the activity, because the activity implements this interface!

Public interface ILoginPresenter {
Public void clear ();
Public void doLogin (String name, String password );
}

Entity class:

Public class User {
Private String name;
Private String password;
Public User (String name, String pwd ){
This. name = name;
This. password = pwd;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
@ Override
Public String toString (){
Return "User [name =" + name + ", password =" + password + "]";
}

}

 

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.