android mvp模式讲解

来源:互联网 发布:仙剑传奇网站源码 编辑:程序博客网 时间:2024/05/17 21:51

最近项目架构要重新设计,会涉及到一些现在比较流行的架构,今天学习下MVP模式,相信大家项目中都用到了,这个模式出来也好几年了,但是在android中好像是去年可以流行起来,MVP模式是从经典的MVC模式出来变换出来的,MVC可能做java开发的人很熟悉,但是在android中实际体现的不够明显,因为在android中把Activity即当做View(视图层)也当做逻辑处理层,就是Model笔记纯粹,比如一个进度条下载肯定要不断的去更新下载的进度,那么在Activity中可能要有这个View的应用,而我们自定义view的构造函数时候一般都要传上下文,这个时候都是传this,所以MVC图如下


发现在MVP中M和V是被隔离了,这就是MVC和MVP最大的不同之处!而V和P之间是依赖接口.

MVP各层职责:

V:负责绘制UI元素、与用户进行交互(在Android中体现为Activity);

M:负责存储、检索、操纵数据(有时也实现一个Model interface用来降低耦合);

P:作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。

还有就是(view和presenter层之间定义的接口)需要View实现的接口,View通过View interface与Presenter进行交互,降低耦合,方便进行单元测试

现在通过获取用户的名字和性别来看下使用MVP怎么写这些代码

首先是Model层:

package com.example.mvp.model;/** * Created by Adminis on 2017/5/30. * Model层 */public interface IModel {    void loadData(OnLoadDataListener listener);    interface  OnLoadDataListener{        void onSuccess(Person p);    }}
具体的数据层:

package com.example.mvp.model;/** * Created by Adminis on 2017/5/30. * Model */public class PersonModel implements IModel {    @Override    public void loadData(OnLoadDataListener listener) {        Person person = new Person();        person.setName("张三");        person.setSex("男");        person.setAge(18);        if(listener!=null){            listener.onSuccess(person);        }    }}
实体:

package com.example.mvp.model;/** * Created by Adminis on 2017/5/30. */public class Person {    private String name;    private int age;    private String img;    private String sex;    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getImg() {        return img;    }    public void setImg(String img) {        this.img = img;    }}
UI接口:

package com.example.mvp.view;import com.example.mvp.model.Person;/** * Created by Adminis on 2017/5/30. */public interface IInterView {       void  showLoading();//显示进度条       void  showData(Person person);//显示数据}
presenter:

package com.example.mvp.presenter;import com.example.mvp.model.IModel;import com.example.mvp.model.Person;import com.example.mvp.model.PersonModel;import com.example.mvp.view.IInterView;/** * Created by Adminis on 2017/5/30. * Presenter */public class PersonPresenter  {     private IInterView iInterView;//持有视图层UI接口引用     private IModel iModel = new PersonModel();//持有Model层的引用    public PersonPresenter(IInterView iInterView) {        this.iInterView = iInterView;    }    public void fetch(){        if(iInterView==null||iModel==null){            return;        }        iInterView.showLoading();        iModel.loadData(new IModel.OnLoadDataListener() {            @Override            public void onSuccess(Person p) {                iInterView.showData(p);//回调视图层            }        });    }}
V(Activity)

package com.example.mvp;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import com.example.mvp.model.Person;import com.example.mvp.presenter.PersonPresenter;import com.example.mvp.view.IInterView;public class MainActivity extends AppCompatActivity implements IInterView {    private TextView tv_name;    private TextView tv_sex;    private PersonPresenter presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        presenter = new PersonPresenter(this);        presenter.fetch();    }    private void initView() {        tv_name = (TextView) findViewById(R.id.tv_name);        tv_sex = (TextView) findViewById(R.id.tv_sex);    }    @Override    public void showLoading() {            //显示加载进度条    }    @Override    public void showData(Person person) {        if(person!=null){            tv_name.setText(person.getName());            tv_sex.setText(person.getSex());        }    }}

项目结构图如下:


MVP模式相比MVC模式有如下几个优点:

1;减少了Activity的职责,简化了Activity中的代码,将复杂的逻辑代码提取到了Presenter中进行处理。与之对应的好处就是,耦合度更低,也能更好的维护,解决bug,可以直接去对应的类找就ok.

2:方便单元进行测试

3:解决内存泄漏

比如现在有一个工具类:

package com.example.mvp.util;import android.content.Context;/** * Created by ricky on 2016/11/2. */public class CommUtil {    private static CommUtil instance;    private Context context;    private CommUtil(Context context){        this.context = context;    }    public static CommUtil getInstance(Context context){        if(instance == null){            instance = new CommUtil(context);        }        return instance;    }}
如果在Activity中直接就这么使用:

CommUtil.getInstance(this);
如果在Activity横竖屏切换的时候会创建多个Activity或者Activity销毁了,但是这个工具类还引用了这个Context,都会出现内存泄漏的问题.

哪怎么解决呢? 使用弱引用

现在对MVP进行一个抽象封装的工作,我们每个presenter对应一个Model,而每个业务对应presenter都不一样,所以应该把所有的presenter的共同地方抽象出来,这样能复用一些代码.

每个presenter中既有V的引用也有M的引用,而你要写一个公共的presenter,肯定是不能关联具体的V或者M,所以只能用泛型代替,

package com.example.mvp.presenter;import com.example.mvp.model.IModel;import com.example.mvp.model.Person;import com.example.mvp.model.PersonModel;import com.example.mvp.presenter.base.BasePresenter;import com.example.mvp.view.IInterView;/** * Created by Adminis on 2017/5/30. * Presenter * T 是UI接口 */public class PersonPresenter<T> extends BasePresenter<IInterView> {     private IInterView iInterView;//持有视图层UI接口引用     private IModel iModel = new PersonModel();//持有Model层的引用    public PersonPresenter(IInterView iInterView) {        this.iInterView = iInterView;    }    @Override    public void fetch(){        if(iInterView==null||iModel==null){            return;        }        iInterView.showLoading();        iModel.loadData(new IModel.OnLoadDataListener() {            @Override            public void onSuccess(Person p) {                iInterView.showData(p);//回调视图层            }        });    }}
我们要新建一个BaseActivity把一些共同的业务直接放在父类中做,子类就没必要做了,

package com.example.mvp;import android.os.Bundle;import android.widget.TextView;import com.example.mvp.model.Person;import com.example.mvp.presenter.PersonPresenter;import com.example.mvp.util.CommUtil;import com.example.mvp.view.IInterView;public class MainActivity extends BaseActivity<IInterView,PersonPresenter<IInterView>> implements IInterView {    private TextView tv_name;    private TextView tv_sex;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        CommUtil.getInstance(this);        initView();        mT.fetch();    }    @Override    protected PersonPresenter<IInterView> createProsenter() {        return new PersonPresenter<>(this);    }    private void initView() {        tv_name = (TextView) findViewById(R.id.tv_name);        tv_sex = (TextView) findViewById(R.id.tv_sex);    }    @Override    public void showLoading() {            //显示加载进度条    }    @Override    public void showData(Person person) {        if(person!=null){            tv_name.setText(person.getName());            tv_sex.setText(person.getSex());        }    }}

类图如下:


总算写完了.