About Android MVP mode

Source: Internet
Author: User



The MVP schema is a bit of a solution:

M--model, business layer (mainly responsible for the implementation of specific functions)

V--view, view layer (used as display)

P--presenter, connecting layer (building model layer and view Layer communication Bridge)


MVP mode, the model layer and the view layer is completely isolated (Zhei), both of the communication is through the presenter layer as a bridge to communicate, so the presenter layer must contain the model layer and the View layer specific instances (from this can be seen, when the interface changes, Just change the view instance in the presenter layer, and when the data load mode changes, just change the model instance in the presenter layer.


Experience:

To write the MVP schema code, I mainly look at the M-layer and V-layer features Overview:

1. First look at the view layer, the view layer is mainly used for display, then, when writing the view layer interface, it is necessary to consider the view layer has the number of display requirements, thus defining the corresponding interface.

2. After viewing the view layer, you must consider the next model layer specific business functions of the implementation, the implementation process, there may be time-consuming state, at this time the state should be notified through an interface to the presenter layer, so that the view layer is also informed that the corresponding display; The same is true for successful failure of the results (that is, the model layer operation results are to be known through the presenter interface, so that the view layer knows).



Eg: the interface has only one textview as the display, want to assume to download the file from the network (simulation for loading data), TextView in the download different process to make different text prompts.

Ideas:

1. View layer: The main 3 processes: Download prompt, download success prompt, download failure prompt.

Com.mvp.view

public interface IView

{

void Onloading ();

void onsuccess (list<string> data);//display after successful download

void onfailed ();

}

2. Model layer: mainly for data loading

Com.mvp.model

public interface IModel

{

Loading data can be an asynchronous operation that informs the presenter result via an interface callback

void LoadData (IPresenter listener);

}

Com.mvp.presenter

public interface IPresenter

{

void LoadData ();//Tell model to start LoadData

void onsuccess (list<string> data);

void onfailed ();

void OnDestroy ();

}



Above, the generic MVP interface is defined, and the follow-up is to make specific implementations for specific layers.


Com.mvp.model

public class IModelImpl01 implements IModel

{

@Override

void LoadData (IPresenter listener)

{

Do download Func,here pausing 10s to imitate the download Behav

try{

Thread.Sleep (10000);

list<string> data = new arraylist<string> ();

Data.add ("Imitate Loading data successfully");

Listener.onsuccess (data);

}catch (Exception e)

{

Listener.onfailed ();

}

}

}

Com.mvp.presenter

public class IPresenterImpl01 implements IPresenter

{

Private IView view;

Private IModel model;

Public IPresenterImpl01 (IView view)

{

This.view = view;

Model = new IMODELIMPL01 ();

}

@Override

void LoadData ()

{

if (view! = null)

{

View.onloading ();

}

Model.loaddata (this);

}

@Override

void onsuccess (list<string> data)

{

if (view! = null)

{

View.onsuccess (data)

}

}

@Override

void Onfailed ()

{

if (view! = null)

{

View.onfailed ();

}

}

@Override

void OnDestroy ()

{

view = null;

}

}

Com.mvp.view

public class Mainactivity extends Activity implements IView

{

Private TextView tvShow;

Private IPresenter presenter;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

TvShow = (TextView) Findviewbyid (r.id.tvshow);

Presenter = new IPresenterImpl01 (this);

Presenter.loaddata ();

}

@Override

void Onloading ()

{

Tvshow.settext ("onloading ...");

}

@Override

void onsuccess (list<string> data)//After a successful download, display

{

Tvshow.settext ("Load Data success:" +data);

}

@Override

void Onfailed ()

{

Tvshow.settext ("Load data failed");

}

@Override

protected void OnDestroy ()

{

Super.ondestroy ();

Presenter.ondestroy ();

}

}





This article is from the "whatever957" blog, make sure to keep this source http://whatever957.blog.51cto.com/6835003/1832694

About Android 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.