Examples of MVP usage in Android

Source: Internet
Author: User
Tags gettext


| Copyright NOTICE: This article is the original article, without the owner's permission to reprint.


1. Introduction

Everyone knows and is familiar with the MVC pattern, the Android native uses the MVC model, but the biggest problem with it is that it's not clear, and if someone asks you what C in MVC in your project is in the middle of an interview, you're definitely not going to want to answer that right now. It represents Controler, If someone asks you the difference between your controler and your view, you'll find that you're getting yourself into it. It's hard to explain, because MVC's biggest flaw in Android is that its controler and view are hard to separate and the whole project is too coupled. And the view and model between can interact between view and Controler can also be between the interaction between this will be chaos. Fortunately, Daniel has studied MVP,MVP is specifically used to solve the problem of unclear MVC responsibilities, where m or representative of the MODEL,V is representative of the View,presenter is acting as a view and model interaction between the middle side, This avoids the original view can interact with the activity, also can interact with the model, model can also interact with the activity of chaos, now MVP view only with presenter interaction, model only with presenter interaction, Clear and transparent responsibilities.
2. Examples

Conceptually, basically, the difference between MVC and MVP. Now we have a detailed explanation of the actual code, first of all we have to understand that the MVP is implemented through the interface callback, so the UI layer, the model layer is required interface implementation.

We use a login example to illustrate this pattern, because landing can be said to be relatively simple and can completely cover most of the examples we need in our MVP. Here is a picture of the landing

1. First of all, MVC or MVP mode beans are certainly essential. This userbean is the user login example, simple and clear is not much to say


public class UserBean {
Account
Private String account;
Password
private String password;

Public UserBean (string account, string password) {
This.account = account;
This.password = password;
}

Public String Getaccount () {

return account;
}

public void Setaccount (String account) {
This.account = account;
}

Public String GetPassword () {
return password;
}

public void SetPassword (String password) {
This.password = password;
}
}


2. Then we need to write, model is the business logic layer, here we still adopt the implementation of the interface, because such internal methods at a glance also facilitate unit testing, all of our business must be used in model,
Instead of being directly used in the activity as many business logic in MVC and requiring view support, this can easily lead to memory leaks, as many asynchronous tasks retire behind the scenes but retain references to current activities.
And the Java recycling mechanism will not be able to recycle the activity in the case of low memory, so the activity referenced by the activity does not have a very easy memory leak.

Model interface

Public interface Iloginbiz {
void Login (String account,string password,onloginlistener listener);
}


Model implementation

public class Loginbiz implements Iloginbiz {
@Override
public void Login (string account, string password, final Onloginlistener listener) {
if (!) ( Account.equals ("") &&password.equals (""))
{
Account password NOT null allow login, login successful
New Thread (New Runnable () {
@Override
public void Run () {
try {
Thread.Sleep (2000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
Listener.loginsucess ();
}
). Start ();
}
Else
{
Account password is blank login failed
Listener.loginfailed ();
}
}
}

3. Then there is the view layer, in which we use activity as a view, significantly different from MVC, but we need to provide the view reference for the activity to presenter, and implement the callback update interface in the activity. The method of callback here we abstract out into an interface

All of the methods inside are used to update the UI
Public interface Iloginview {
String Getaccount ();
String GetPassword ();
void Loginsucess ();
void LoginFailed ();
}


Some people would ask, "Is it all a way to update the UI?" Why there are Getaccount () and GetPassword () methods, it's easy to think because your view is interacting with the model layer in presenter rather than in the activity, Then your account password must not be used directly.
Edttext.gettext (). toString (), so you need a way to get to account and password.

4. Finally, the presenter layer is used to control the interaction between view and model, and you need to know that you need to use a handler in the callback function of the biz. Post (new Runnable) to replace the UI (Handler.post () and Handler.sendmessage () are used to notify),
The method in the new runable here is to update the UI's operation, which is the UI thread, and don't understand

How to re-opened a thread again. The view's interface callback allows you to do some updating in the activity,

public class Userloginpre {
Private Iloginview view;
Private Iloginbiz loginbiz;
Private Handler handler=new Handler ();
Public Userloginpre (Iloginview Iloginview) {
View=iloginview;
Loginbiz=new loginbiz ();
}
public void Login ()
{
Loginbiz.login (View.getaccount (), View.getpassword (), new Onloginlistener () {
@Override
public void loginsucess () {
Update UI Thread
Handler.post (New Runnable () {
@Override
public void Run () {
View.loginsucess ();
}
});
}

@Override
public void loginfailed () {
Handler.post (New Runnable () {
@Override
public void Run () {
View.loginfailed ();
}
});
}
});
}
}

Have you seen in the Userloginpre () constructor We've put both view and model in the presenter so that the view and model interact in the presenter, and then the update is implemented through the callback function.

5. Finally, the activity in the code for some update operations to achieve a model,view separation.

The activity in the MVP is to implement the view layer interface and then interact with the presenter
public class Loginactivity extends Appcompatactivity implements Iloginview, View.onclicklistener {

Private EditText Edt_account;
Private EditText Edt_password;
Private Button Btn_login;
Private ProgressBar ProgressBar;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_login);
Initview ();
Setlistener ();
}

public void Initview ()
{
Edt_account = (edittext) Findviewbyid (R.id.edt_account);
Edt_password = (edittext) Findviewbyid (R.id.edt_password);
Btn_login = (Button) Findviewbyid (R.id.btn_login);
ProgressBar = (ProgressBar) Findviewbyid (R.id.progress_bar);
}
public void Setlistener ()
{
Btn_login.setonclicklistener (this);
}
public void Login ()
{
Userloginpre loginpre=new Userloginpre (this);
Progressbar.setvisibility (view.visible);
Loginpre.login ();
}

@Override
Public String Getaccount () {
Return Edt_account.gettext (). toString ();
}

@Override
Public String GetPassword () {
Return Edt_password.gettext (). toString ();
}

@Override
public void loginsucess () {
Progressbar.setvisibility (View.gone);
Toast.maketext (This, "Login successful", Toast.length_short). Show ();
}

@Override
public void loginfailed () {
Progressbar.setvisibility (View.gone);
Toast.maketext (This, login failed, toast.length_short). Show ();
}

@Override
public void OnClick (View v) {
Login ();
}
}

3. Summary

MVC and the difference between the MVP we also explained that we should also be from the code to feel the difference, the specific use or need to write their own examples to use, then the MVP so good have any shortcomings, in fact, or some from the amount of code can be seen a lot more,

Because of the use of a large number of interfaces and a presenter layer, the UI and the business must be written as an interface, but this is also good, the advantage is that you have to write the code before you must understand the business logic, not as before a complex page

The activity of the code is thousands of lines, reading difficult and high coupling is not easy to modify, and many can be pulled out of the way no longer use other pages.


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.