一、MVP与MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写
MVP 全称:Model-View-Presenter
MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示。
MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,而在MVC中View会直接从Model中读取数据而不是通过 Controller。
二、MVP模式实现方式
三、实例
现在通过一个实例来对MVP模式有个初步了解。
程序逻辑:将一段网络请求的字符串显示在界面
1、MVP之V:UI实现view的方法,引用Presenter
UI
package com.wj.myapplication.ui;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;
import com.wj.myapplication.R;
import com.wj.myapplication.model.MvpModelBean;
import com.wj.myapplication.presenter.MvpPresenter;
import com.wj.myapplication.view.MvpView;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* for: mvp简单使用 Presenter负责逻辑的处理,Model提供数据,View负责显示
* MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,
* 它们之间的通信是通过Presenter (MVC中的Controller)来进行的,
* 所有的交互都发生在Presenter内部,而在MVC中View会直接从Model中读取数据而不是通过 Controller。
* UI实现View方法,引用Presenter
* Created by wj on 2017/7/10.
*/
public class MVPActivity extends Activity implements MvpView{
@Bind(R.id.tv_mvp)
TextView tvMvp;
private MvpPresenter mvpPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvp);
ButterKnife.bind(this);
mvpPresenter = new MvpPresenter(this);
//通知presenter获取数据
mvpPresenter.loadData();
}
/**
* 将数据显示在界面
* @param mvpModelBean
*/
@Override
public void showStr(MvpModelBean mvpModelBean) {
tvMvp.setText(mvpModelBean.getStr());
}
}
view
package com.wj.myapplication.view;
import com.wj.myapplication.model.MvpModelBean;
/**
* for: 处理界面业务需要哪些方法
* Created by wangjian on 2017/7/10.
*/
public interface MvpView {
/**
* mvp页面的业务只有一个,显示一段话
*/
void showStr(MvpModelBean mvpModelBean);
}
2、MVP之P:Presenter调用model实现数据获取
IMVPPresenter
package com.wj.myapplication.presenter;
import com.wj.myapplication.model.MvpModelBean;
/**
* for: 此接口的作用是连接model
* Created by wangjian on 2017/7/10.
*/
public interface IMvpPresenter {
void loadDataSuccess(MvpModelBean mvpModel);
void loadDataFail();
}
Presenter
package com.wj.myapplication.presenter;
/**
* for: 连接view
* Created by wangjian on 2017/7/10.
*/
public interface Presenter<V> {
void attach(V view);
void detach();
}
MVPPresenter
package com.wj.myapplication.presenter;
import com.wj.myapplication.model.MVPModel;
import com.wj.myapplication.model.MvpModelBean;
import com.wj.myapplication.view.MvpView;
/**
* for:View和Model的桥梁,它从Model层检索数据后,返回给View层
* Created by wangjian on 2017/7/10.
*/
public class MvpPresenter implements IMvpPresenter,Presenter<MvpView>{
/**
* view来自view层引用presenter
* @param view
*/
private MvpView mvpView;
/**
* model通过创建一个model的引用
*/
private MVPModel mvpModel;
public MvpPresenter(MvpView view) {
attach(view);
mvpModel = new MVPModel(this);
}
/**
* 通知model去获取数据
*/
public void loadData(){
mvpModel.loadData();
}
@Override
public void attach(MvpView view) {
this.mvpView = view;
}
@Override
public void detach() {
this.mvpView = null;
}
/**
* 通知view去改变界面
* @param mvpModel
*/
@Override
public void loadDataSuccess(MvpModelBean mvpModel) {
mvpView.showStr(mvpModel);
}
@Override
public void loadDataFail() {
}
}
3、MVP之M:Model逻辑实现,回调Presenter方法
model
package com.wj.myapplication.model;
import com.wj.myapplication.presenter.IMvpPresenter;
/**
* for: Model逻辑实现,回调Presenter方法
* Created by wangjian on 2017/7/10.
*/
public class MVPModel {
private IMvpPresenter iMvpPresenter;
public MVPModel(IMvpPresenter iMvpPresenter){
this.iMvpPresenter = iMvpPresenter;
}
/**
* 获取数据并回调
*/
public void loadData(){
MvpModelBean mvpModelBean = new MvpModelBean();
//可以通过网络获取数据
mvpModelBean.setStr("this string is from MVP ");
iMvpPresenter.loadDataSuccess(mvpModelBean);
}
}
modelbean
package com.wj.myapplication.model;
/**
* for:
* Created by wangjian on 2017/7/10.
*/
public class MvpModelBean {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}