What is MVP? MVP is actually an architectural pattern, which is the evolution of “MVC”. When I downloaded the demo for the first time, I was confused by it. (a blog here: http://blog.csdn.net/lmj623565791/article/details/46596109, Zhang Hongyang written a great god, is very good)

M: Provide data, entity schema, or process some business logic.

V: view, which is presented to and interacts with the user.

P: coordinator, a bridge between M and V, is responsible for completing their interaction.

Here embezzled hongyang daishen two pictures, please forgive me too lazy to draw, ha ha.




This diagram clearly shows the entire MVP process, and another is the difference between MVC and MVP, which is also easy to understand. Please see the following diagram:




Well, that’s the theory, and here’s how it works:

MVP starts with three base classes (interfaces) :

public interface BaseModel {

}


public interface BaseView {

    Activity getActivity();

}

      

public abstract class BasePresenter<T extends BaseView, M extends BaseModel> {



    public T mView;

    public M mModel;





     public BasePresenter(T view, M model) {

        if (null == view) {

           throw new NullPointerException(“view must not null”);

        }

         this.mView = view;

        this.mModel = model;

        }

public void detachView() { mView = null; }}


There is also a base class for the Activity:

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {





    protected T mPresenter;





    public abstract T createPresenter();





    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        mPresenter = createPresenter();

        super.onCreate(savedInstanceState);

    }





    public Activity getActivity() {

        return this;

    }





    @Override

    protected void onDestroy() {

        if (null != mPresenter) {

            mPresenter.detachView();

        }

        super.onDestroy();

    }

}

The structure of the project is shown below:




Where the MainActivity is V, the MainModel is M, and the MainPresenter is P, in addition to the MainContract class, the code is as follows:

public interface MainContract { interface View extends BaseView { void setValue(String value); } interface Model extends BaseModel { void getValue(ICallBack iCallBack, Context context); } abstract class Presenter extends BasePresenter

{ public Presenter(View view, Model model) { super(view, model); } public abstract void getValue(); }}
,>


This class defines the M, V, and P interfaces or classes we need. MainActivity implements the View interface as the V layer, MainModel implements the Model interface as the M layer, and MainPresenter inherits the abstract class Presenter as the P layer. Now let’s look at the Model code:

public class MainModel implements MainContract.Model { @Override public void getValue(final ICallBack iCallBack, final Context context) { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } iCallBack.success(context.getString(R.string.value)); } }).start(); }}

The model simply returns a string, which we’ll use as a callback for convenience.

Here is the View code:


package com.sonny.mvp; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.sonny.mvp.base.BaseActivity; public class MainActivity extends BaseActivity

implements MainContract.View, View.OnClickListener { private TextView mTvValue; @Override public MainContract.Presenter createPresenter() { return new MainPresenter(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_get_data).setOnClickListener(this); mTvValue = (TextView) findViewById(R.id.tv_mvp_value); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_get_data: mPresenter.getValue(); break; } } @Override public void setValue(final String value) { runOnUiThread(new Runnable() { @Override public void run() { mTvValue.setText(value); }}); }}

Since P acts as a bridge between M and V, it has an instance of M and V, so when a View instantiates a Presenter, we pass the View to it, thus associating the P layer with the V layer.

Here is the code for the P layer:

public class MainPresenter extends MainContract.Presenter { public MainPresenter(MainContract.View view) { super(view, new MainModel()); } @Override public void getValue() { mModel.getValue(new ICallBack() { @Override public void success(String value) { mView.setValue(value); } }, mView.getActivity()); }}

This layer holds objects of V and M.

The getValue method calls the M method and returns the data. If it succeeds, it calls the V method to display the data. In this way, our Presenter completes the M and V interaction.

Code me on: https://github.com/linqssonny/MVP, welcome everyone to download to consult, please correct me if there is any error, QQ: 252624617.


The article lists