1.什么是MVP?
简单理解:就是通过Presenter将View和Model解耦
M —>Model 包括:与数据相关都属于M层(例如:数据库、文件、网络、数据解析、数据保存......)
V —>View 包括:在MVC中View只是一个单纯视图,但是在MVP中(例如:Activity、Fragment、布局)
P —>Presenter 包括:调度,通过P层将我们的View层和Model层进行关联转换
2.MVP和设计模式有什么区别?
举例说明:北京国贸三期(整体架构) --- MVP
国贸三期中 窗户设计、电梯设计、走廊设计 ...... 相当于设计模式 --- 针对具体的问题或者场景提出不同的解决方案。
3.MVP架构交互过程
- 简单MVP登录案例
public interface MainView {
public void onLoginResult(String result);
}
public class MainActivity extends AppCompatActivity implements MainView {
private MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mainPresenter = new MainPresenter(this);
}
/**
* 其实在Android当中,本身就是一个非常典型的MVC架构
* 在Android MVC中
* M代表:数据
* C代表:activity或者Fragment
* V代表:视图
*/
//MVP适合大项目
//MVP更加便于团队开发
public void click(View v){
this.mainPresenter.login("kpioneer","123456");
}
@Override
public void onLoginResult(String result) {
Toast.makeText(this,result, Toast.LENGTH_LONG).show();
}
/**
* Created by Xionghu on 2017/7/11.
* Desc: M层(数据、网络)
*/
public class MainModel {
public void login(String username, String pwd, final HttpUtils.OnHttpResultListener onHttpResultListener){
HttpTask httpTask = new HttpTask(new HttpUtils.OnHttpResultListener() {
@Override
public void onResult(String result) {
//解析数据
//更新UI
onHttpResultListener.onResult(result);
}
});
httpTask.execute(username,pwd,"http://www.baidu.com");
}
}
public class MainPresenter {
private MainView mainView;
private MainModel mainModel;
public MainPresenter(MainView mainView) {
this.mainView = mainView;
this.mainModel = new MainModel();
}
public void login(String userName, String pwd){
this.mainModel.login(userName, pwd, new HttpUtils.OnHttpResultListener() {
@Override
public void onResult(String result) {
mainView.onLoginResult(result);
}
});
}
5.分析简单MVP登录案例的问题?
问题1:
假设Activty意外关闭,这个时候网络请求还在进行,当数据返回的时候,发现Activity(或者Fragment) 挂掉了将会造成内存泄漏。
解决办法:MainPresenter提供一个销毁mainView的方法
问题2:
项目开发当中Activity或者Fragment数量很庞大 Presenter相关类将会造成代码冗余。
解决办法: 单独抽象出来(引出抽象类)
抽象类设计MVP架构
抽象 AbsMvpPresenter
public abstract class AbsMvpPresenter<V extends IMvpView> {
private V view;
public V getView() {
return view;
}
/**
* 绑定
* @param view
*/
public void attachView(V view) {
this.view = view;
}
/**
* 解决绑定
*/
public void detachView(){
this.view =null;
}
}
public interface IMvpView {
}
public class MainPresenter extends AbsMvpPresenter<MainView> {
private MainModel mainModel;
public MainPresenter() {
this.mainModel = new MainModel();
}
public void login(String userName, String pwd){
this.mainModel.login(userName, pwd, new HttpUtils.OnHttpResultListener() {
@Override
public void onResult(String result) {
if(getView()!=null){
getView().onLoginResult(result);
}
}
});
}
}
public abstract class MvpActivity<V extends IMvpView, P extends AbsMvpPresenter<V>> extends Activity {
private P presenter;
private V view;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (presenter == null) {
this.presenter = bindPresenter();
}
if (view == null) {
this.view = bindView();
this.presenter.attachView(this.view);
}
}
public P getPresenter() {
return presenter;
}
public V getView() {
return view;
}
public abstract P bindPresenter();
public abstract V bindView();
@Override
protected void onDestroy() {
super.onDestroy();
if (this.presenter != null) {
this.presenter.detachView();
}
}
}
public class MainActivity extends MvpActivity<MainView,MainPresenter> implements MainView {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public MainPresenter bindPresenter() {
return new MainPresenter();
}
@Override
public MainView bindView() {
return this;
}
/**
* 其实在Android当中,本身就是一个非常典型的MVC架构
* 在Android MVC中
* M代表:数据
* C代表:activity或者Fragment
* V代表:视图
*/
//MVP适合大项目
//MVP更加便于团队开发
public void click(View v){
getPresenter().login("kpioneer","123456");
}
@Override
public void onLoginResult(String result) {
Toast.makeText(this,result, Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
问题三:
6.搭建MVP架构
public class MainPresenter extends MvpBasePresenter<MainView> {
private MainModel mainModel;
public MainPresenter(Context context) {
super(context);
this.mainModel = new MainModel();
}
public void login(String username, String pwd) {
this.mainModel.login(username, pwd, new HttpUtils.OnHttpResultListener() {
@Override
public void onResult(String result) {
if(isAttachView()){
getView().onLoginResult(result);
}
}
});
}
}
问题:每一次在Presenter中调用方法都需要处理空(isAttachView())判断
假设:50个Activity,20个Presenter,每一个Presenter里面有20个方法,做判断400次?
解决办法 ----- 动态代理模式
每当调用这个方法的时候,我就要去监听
public interface MvpPresenter<V extends MvpView> {
/**
* 绑定视图
* @param view
*/
public void attachView(V view);
public void dettachView();
}
public class MvpBasePresenter<V extends MvpView> implements MvpPresenter<V> {
private WeakReference<Context> weakContext;
private WeakReference<V> weakView;
private V proxyView;
public MvpBasePresenter(Context context) {
this.weakContext = new WeakReference<Context>(context);
}
public Context getContext() {
return weakContext.get();
}
public V getView() {
return proxyView;
}
/**
* 用于检测View是否为空对象
*
* @return
*/
public boolean isAttachView() {
if (this.weakView != null && this.weakView.get() != null) {
return true;
}
return false;
}
@Override
public void attachView(V view) {
this.weakView = new WeakReference<V>(view);
MvpViewInvocationHandler invocationHandler = new MvpViewInvocationHandler(this.weakView.get());
//在这里采用动态代理
proxyView = (V) Proxy.newProxyInstance(view.getClass().getClassLoader(), view.getClass().getInterfaces(), invocationHandler);
}
@Override
public void dettachView() {
if (weakView.get() != null) {
this.weakView.clear();
this.weakView = null;
}
}
private class MvpViewInvocationHandler implements InvocationHandler {
private MvpView mvpView;
public MvpViewInvocationHandler(MvpView mvpView) {
this.mvpView = mvpView;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (isAttachView()) {
return method.invoke(mvpView, args);
}
return null;
}
}
}
关键代码
public void attachView(V view) {
this.weakView = new WeakReference<V>(view);
MvpViewInvocationHandler invocationHandler = new MvpViewInvocationHandler(this.weakView.get());
//在这里采用动态代理
proxyView = (V) Proxy.newProxyInstance(view.getClass().getClassLoader(), view.getClass().getInterfaces(), invocationHandler);
}
private class MvpViewInvocationHandler implements InvocationHandler {
private MvpView mvpView;
public MvpViewInvocationHandler(MvpView mvpView) {
this.mvpView = mvpView;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (isAttachView()) {
return method.invoke(mvpView, args);
}
return null;
}
}