android mvp模式,android MVP模式简介

MVP架构略解:

M--Model,业务层(主要负责具体功能实现)

V--View,视图层(用作显示)

P--Presenter,连接层(搭建Model层和View层通信桥梁)

MVP模式下,Model层和View层是完全隔离(解偶)的,两者的通信都是通过Presenter层作为桥梁来进行通信的,所以,Presenter层中一定含有Model层和View层具体实例(由这可以看出,当界面改变时,只需更改Presenter层中的View实例即可;同理,当数据加载方式改变时,只需更改Presenter层中的Model实例即可.

心得:

要写出MVP架构代码,主要是看M层和V层功能概述:

1.首先看View层,View层主要做显示用,那么,写View层接口的时候,就要考虑下View层有多少种显示需求,从而定义出相应接口.

2.看完View层后,就要考虑下Model层具体业务功能的实现了,实现过程中,有可能处于耗时状态,此时的状态就应该通过某个接口通知到Presenter层,从而让View层也得知,做出相应显示;实现结果成功失败也是如此(也就是说,Model层操作结果要通过Presenter的接口让Presenter知道,从而让View层知道).

eg:界面只有一个TextView用作显示,想假设从网络上下载文件(模拟为加载数据),TextView在下载不同过程进行不同的文字提示.

思路:

1.View层:主要分3个过程:下载时提示,下载成功提示,下载失败提示.

//com.mvp.view

public interface IView

{

void onLoading();

void onSuccess(List data);//数据下载成功后,进行显示

void onFailed();

}

2. Model层:主要进行数据加载

//com.mvp.model

public interface IModel

{

//加载数据可能是异步操作,通过接口回调告知Presenter结果

void loadData(IPresenter listener);

}

//com.mvp.presenter

public interface IPresenter

{

void loadData();//告诉Model开始loadData

void onSuccess(List data);

void onFailed();

void onDestroy();

}

以上,就将通用的MVP接口定义出来的,后续要做的就是针对特定层做出特定实现即可.

//com.mvp.model

public class IModelImpl01 implements IModel

{

@Override

void loadData(IPresenter listener)

{

//do download func,here pausing 10s to imitate the download behav

try{

Thread.sleep(10000);

List data = new ArrayList();

data.add("imitate loading data successfully");

listener.onSuccess(data);

}catch(Exception e)

{

listener.onFailed();

}

}

}

//com.mvp.presenter

public class IPresenterImpl01 implements IPresenter

{

private IView view;

private IModel model;

public IPresenterImpl01(IView view)

{

this.view = view;

model = new IModelImpl01();

}

@Override

void loadData()

{

if(view != null)

{

view.onLoading();

}

model.loadData(this);

}

@Override

void onSuccess(List data)

{

if(view != null)

{

view.onSuccess(data)

}

}

@Override

void onFailed()

{

if(view != null)

{

view.onFailed();

}

}

@Override

void onDestroy()

{

view = null;

}

}

//com.mvp.view

public class MainActivity extends Activity implements IView

{

private TextView tvShow;

private IPresenter presenter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tvShow = (TextView)findViewById(R.id.tvShow);

presenter = new IPresenterImpl01(this);

presenter.loadData();

}

@Override

void onLoading()

{

tvShow.setText("onLoading...");

}

@Override

void onSuccess(List data)//数据下载成功后,进行显示

{

tvShow.setText("load data success:"+data);

}

@Override

void onFailed()

{

tvShow.setText("load data failed");

}

@Override

protected void onDestroy()

{

super.onDestroy();

presenter.onDestroy();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值