Android设计模式之MVP

2019-03-22  本文已影响0人  极客天空

引言

在Android的架构中Activity,fragment,布局的xml相当于View。然而在实际的开发过程中,Android的View层任务繁重,将V和C都糅杂在Activity、Fragment中,这就导致了在实际开发中View层太过累赘。MVP把Activity中的UI逻辑抽象成View接口,把业务逻辑抽象成Presenter接口,Model类还是原来的Model


mvc1.png
mvp1.png

MPV的介绍

相对而言,MVP模式的代码量就多了,类文件也多了,简单的一个业务逻辑操作就要各方来配合协作(即是需要presenter 和 view的接口)。但是这个问题完全在可以接收的范围。完全符合Java的抽象封装设计原则(接口隔离,开闭原则)

通过实例介绍MPV的使用

mvp展示.gif
package com.rrcc.mvp.mvpdemo.view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.rrcc.mvp.mvpdemo.R;
import com.rrcc.mvp.mvpdemo.adapter.DailyPagerAdapter;
import com.rrcc.mvp.mvpdemo.model.OrderListBean;
import com.rrcc.mvp.mvpdemo.model.OrdersModelImpl;
import com.rrcc.mvp.mvpdemo.presenter.IPresenterImpl;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ISearchView, View.OnClickListener{
    private RecyclerView mRecyclerView;
    private DailyPagerAdapter mDailyPagerAdapter = null;
    private List<OrderListBean.DataBean> mList = new ArrayList<OrderListBean.DataBean>();
    private Button mSearchBtn = null;
    private Button mCancelBtn = null;
    private IPresenterImpl mIPresenterImpl = null;
    private OrdersModelImpl mOrdersModelImpl = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mOrdersModelImpl = new OrdersModelImpl();
        mIPresenterImpl = new IPresenterImpl(mOrdersModelImpl,this);
        initView();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_search:
                mIPresenterImpl.doSearch();
                break;
            case R.id.btn_search_cancel:
                mIPresenterImpl.cancelSearch();
                break;
        }


    }

    @Override
    public void showResult(List<OrderListBean.DataBean> datas) {
       if(null!=datas)
           setView(datas);
    }

    @Override
    public void cancelShow(List<OrderListBean.DataBean> datas) {
        if(null!=datas)
            setView(datas);
    }

    private void initView(){
        mRecyclerView = findViewById(R.id.pager_shopping_recycler);
        mSearchBtn = findViewById(R.id.btn_search);
        mCancelBtn = findViewById(R.id.btn_search_cancel);

        mSearchBtn.setOnClickListener(this);
        mCancelBtn.setOnClickListener(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

    }

    private void setView(List<OrderListBean.DataBean> mList){
        mDailyPagerAdapter = new DailyPagerAdapter(this,mList);
        mRecyclerView.setAdapter(mDailyPagerAdapter);
        mDailyPagerAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIPresenterImpl = null;
    }
}
public interface IPresenter {
    void doSearch();
    void cancelSearch();
}
package com.rrcc.mvp.mvpdemo.presenter;
import com.rrcc.mvp.mvpdemo.model.OrderListBean;
import com.rrcc.mvp.mvpdemo.model.OrdersModelImpl;
import com.rrcc.mvp.mvpdemo.view.ISearchView;
import java.util.ArrayList;
import java.util.List;
public class IPresenterImpl implements IPresenter {
    private OrdersModelImpl mOrdersModelImpl = null;
    private ISearchView mSearchView = null;
    private List<OrderListBean.DataBean> mList = null;

    public IPresenterImpl(final OrdersModelImpl ordersModelImpl,final ISearchView searchView) {
        this.mOrdersModelImpl = ordersModelImpl;
        this.mSearchView = searchView;
        mList = new ArrayList<OrderListBean.DataBean>();
    }

    @Override
    public void doSearch() {
        mList = mOrdersModelImpl.getOrderList();
        mSearchView.showResult(mList);
    }

    @Override
    public void cancelSearch() {
        if(mList!=null)
            mList.clear();
        mSearchView.cancelShow(mList);
    }
}

从代码可以看出,IPresenterImpl保留了ISearchView的引用,因此在IPresenterImpl里就可以直接进行UI操作了,而不用在Activity里完成。这里使用了ISearchView引用,而不是直接使用Activity,这样一来,如果在别的Activity里也需要用到相同的业务逻辑,就可以直接复用IPresenterImpl类了。

package com.rrcc.mvp.mvpdemo.view;
import com.rrcc.mvp.mvpdemo.model.OrderListBean;
import java.util.List;
public interface ISearchView {
   void showResult(List<OrderListBean.DataBean> data);
   void cancelShow(List<OrderListBean.DataBean> data);
}

项目代码奉上https://github.com/JLhaoran/mvpdemo.git

上一篇下一篇

猜你喜欢

热点阅读