MVP architecture, mvp architecture android

Source: Internet
Author: User

MVP architecture, mvp architecture android
I. Introduction

The Model View Presenter architecture evolved from the famous Model View Controller Architecture. For Android applications, the development can be regarded as the MVC Architecture, the layout file is regarded as the View, and the Activity is regarded as the Controller, but the Activity also controls the layout update. Therefore, the Activity is the combination of the Controller and the View, the result is that the Actiivty code is also messy, and the MVP is to separate the Controller played by the Activity from the View role.
  View:The View layer is also the View layer. In the View layer, it only displays data and provides a friendly interface for interaction with users. In Android development, Activity or Fragment is usually used as the View layer.
  Model:The Model layer is also the data layer. It is different from the Model in MVC, where it is not just a data Model. In the MVP architecture, the Model is responsible for data access, such as data reading and network data requests.
  Presenter:The Presenter layer is also a bridge between the View layer and the Model layer and processes the business logic. In the MVP architecture, the Model and View cannot interact directly. Therefore, in the Presenter layer, it will obtain the required data from the Model layer, perform some appropriate processing, and then hand it to the View layer for display. In this way, the Presenter isolates the View from the Model, so that there is no coupling between the View and Model, and colleagues also extract the business logic from the View.

In the MVP architecture, these three layers are abstracted into their respective interfaces. The layers are isolated through interfaces, and the Presenter's dependency on View and Model is also dependent on their respective interfaces. This conforms to the interface isolation principle, and is also oriented to interface programming. The Presenter layer contains a View Interface and relies on the Model interface to associate the Model layer with the View layer. For the View layer, a Presnter member variable is held and only the call to the Presenter interface is retained. The specific business logic is handled by the Presnter interface implementation class.

Ii. Benefits

1. View and Model are connected to each other through Presenter instead of directly interacting. The View does not contain a Model, so there is no business logic.
2. The interaction between Presenter and View is implemented through interfaces, which has a low coupling degree and is also conducive to unit testing.
3. Presenter is behavior-based. A Presnter can be used for multiple views, enhancing code reuse.

Iii. Use

Use the MVP architecture to display a ListView.
1. Define a Mode Interface

public interface IMode {    List<String> getDatas();}

2. Define an IView Interface

public interface IView {    void showProgress();    void hideProgress();    void showText(String text);    void showDatas(List<String> datas);    void showToast(String message);}

3. Define a Presenter Interface

public interface IPresenter {    void getDataList();    void onItemClick(int position);}

4. Define the MainMode class to implement the IMode Interface

public class MainMode implements IMode {    @Override    public List<String> getDatas() {        List<String> datas = new ArrayList<>();        //return datas;        datas.add("A");        datas.add("B");        datas.add("C");        datas.add("D");        datas.add("E");        datas.add("F");        return datas;        //return null;    }}

5. Define the MainPresenter class to implement the IPresenter Interface

Public class MainPresenter implements IPresenter {private IView mView; private IMode mMode; public MainPresenter (IView mView) {this. mView = mView; mMode = new MainMode () ;}@ Override public void getDataList () {mView. showProgress (); new Handler (). postDelayed (new Runnable () {@ Override public void run () {List <String> datas = mMode. getDatas (); mView. hideProgress (); if (datas = null) {// failed to get data mVi Ew. showText ("data request failed! ");} Else {// data obtained successfully if (datas. size () = 0) {// data is empty mView. showText (" data is empty! ");} Else {mView. showDatas (datas) ;}}}, 2000) ;}@ Override public void onItemClick (int position) {mView. showToast (String. format ("Position % d clicked", position + 1 ));}}

6. Define the MainActivity class to implement the IView Interface

public class MainActivity extends AppCompatActivity implements IView, AdapterView.OnItemClickListener {    private ListView mListView;    private ProgressBar mProgressBar;    private TextView mTextView;    private IPresenter mPresenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mListView = (ListView) findViewById(R.id.list);        mProgressBar = (ProgressBar) findViewById(R.id.progress);        mTextView = (TextView) findViewById(R.id.text);        mPresenter = new MainPresenter(this);        mPresenter.getDataList();    }    @Override    public void showProgress() {        mProgressBar.setVisibility(View.VISIBLE);        mListView.setVisibility(View.GONE);    }    @Override    public void hideProgress() {        mProgressBar.setVisibility(View.GONE);        mListView.setVisibility(View.VISIBLE);    }    @Override    public void showText(String text) {        mProgressBar.setVisibility(View.GONE);        mListView.setVisibility(View.GONE);        mTextView.setVisibility(View.VISIBLE);        mTextView.setText(text);    }    @Override    public void showDatas(List<String> datas) {        mListView.setAdapter(new ArrayAdapter<>(this, R.layout.simple_list_item, datas));    }    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        mPresenter.onItemClick(position);    }    @Override    public void showToast(String message) {        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();    }}


Code address: Https://github.com/ZhangMiao147/FrameworkDemo

 

References:
Http://www.lai18.com/content/7010875.html
Http://www.lai18.com/content/2453405.html
Http://www.lai18.com/content/475341.html
Http://www.lai18.com/content/24630178.html
Http://www.lai18.com/content/719388.html
Http://www.lai18.com/content/7017296.html

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.