作者:chunhuai | 来源:互联网 | 2023-09-24 17:48
MVP开发模式的理解,以及简单代码讲解
最近在网上看的MVP文章,根据自己的理解,特在此发篇文章记录下,这里我采用的是反向流程,文章如有需要优化之处,欢迎指导。也可加群进行讨论:855207609
在此啰嗦一句,首先看图,拿登录功能打比方说:
1、在View中调用P层(带输入数据)进行加载数据(此时还没请求接口)
2、在P中创建View和Model对象(此时的model层还没有创建,可以先省略)然后创建有参构造方法(View)
3、Model请求完接口后,将数据通过回调接口返回到P层,P在返回给VIew进行显示
项目结构图
MVP理解
1.Model层:处理业务逻辑数据,如加载网络数据等(方法以及接口)
2.View层:当P和V层执行完数据加载后进行回调给主线程进行显示(纯接口)
3.Presenter层:是M和V的桥梁,是去小树林的必经之路。将View传递的请求数据传递给Model进行使用,当M请求完接口后,通过回调接口返回给P,此时P层通过已实例化的View将数据射入显示回调方法中进行UI显示。
View层
这里我把弹框抽离出来—使之公用;当然你也可以写在对应View层
作用:在主线程中进行implement此两个View用于最后的数据回调显示
public interface DialogView {
void showDialog ( String message) ;
void dismissDialog ( ) ;
}
public interface LoginView {
void showData ( String result) ;
}
View层-Activity主线程调用:
首先实现View回调方法,然后调用P层方法,此时P层还没有创建
public class Login extends AppCompatActivity implements DialogView , LoginView {
@BindView ( R . id. et_Log_UserName)
AutoCompleteTextView etLogUserName;
@BindView ( R . id. et_Log_PassWord)
EditText etLogPassWord;
@BindView ( R . id. bt_Loading)
Button btLoading;
@BindView ( R . id. email_login_form)
LinearLayout emailLoginForm;
private ProgressDialog mProgressDialog;
private Handler mHandler;
private Context mContext;
private LoginPresenter loginPresenter;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_login) ;
ButterKnife. bind ( this ) ;
init ( ) ;
MyClick ( ) ;
}
private void init ( ) {
mContext= Login. this ;
if ( loginPresenter== null ) {
loginPresenter= new LoginPresenter ( this , this ) ;
}
mHandler= new Handler ( ) {
@Override
public void handleMessage ( Message msg) {
super . handleMessage ( msg) ;
switch ( msg. what) {
case 1 :
Intent intent= new Intent ( Login. this , Menu. class ) ;
intent. putExtra ( "result" , msg. obj. toString ( ) ) ;
startActivityForResult ( intent, 1 ) ;
break ;
default :
break ;
}
}
} ;
}
private void MyClick ( ) {
btLoading. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
loginPresenter. reqLogin ( "test" , "123" ) ;
}
} ) ;
}
@Override
public void showData ( String result) {
Message msg= new Message ( ) ;
msg. what= 1 ;
msg. obj= result;
mHandler. sendMessage ( msg) ;
}
@Override
public void showDialog ( String message) {
if ( mProgressDialog== null ) {
mProgressDialog= new ProgressDialog ( Login. this ) ;
}
mProgressDialog. setMessage ( message) ;
mProgressDialog. show ( ) ;
}
@Override
public void dismissDialog ( ) {
mProgressDialog. dismiss ( ) ;
}
}
Presenter层:
首先implement《P层》以及LoginModel对象等(此时还没有创建,所以我们要先去创建接口,然后在此实现其方法)》
public class LoginPresenter implements LoginIPresenter {
private DialogView mDialog;
private LoginView mLoginView;
private LoginModel loginModel;
public LoginPresenter ( DialogView mDialog, LoginView mLoginView) {
this . mDialog= mDialog;
this . mLoginView= mLoginView;
if ( loginModel== null ) {
loginModel= new LoginModel ( ) ;
}
}
}
public interface LoginIPresenter {
void reqLogin ( String userName, String passWord) ;
}
Model层:
reqLogin方法中的LoginModel.LoginCallBack参数,可以先写上。此处是用于当请求完接口时直接回调给P层(如果有疑问,可以往下看,到P层可以看到请求方法中实现了一个 回调参数)
public interface LoginMode {
void reqLogin ( String userName, String passWord, LoginModel. LoginCallBack callBack) ;
}
Model层:
此时reqLogin方法中的参数添加了成功或失败回调接口,然后整体看一下是不是感觉,就像以前写在activity中的请求方法
public class LoginModel implements LoginMode {
public LoginView loginView;
@Override
public void reqLogin ( String userName, String passWord, final LoginCallBack callBack) {
new Thread ( ) {
@Override
public void run ( ) {
super . run ( ) ;
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
int code= 0 ;
if ( code== 0 ) {
callBack. onSuccess ( "登录成功" ) ;
} else {
callBack. onError ( "登录失败,用户名或密码错误" ) ;
}
}
} . start ( ) ;
}
public interface LoginCallBack {
void onSuccess ( String result) ;
void onError ( String error) ;
}
}
Presenter层:
当P接口和M层创建完成后,创建弹框DialogView,登录LoginView,和登录LoginModel对象,然后创建有参构造并实例化View和Model并实例化,实现其方法
public class LoginPresenter implements LoginIPresenter {
private DialogView mDialog;
private LoginView mLoginView;
private LoginModel loginModel;
public LoginPresenter ( DialogView mDialog, LoginView mLoginView) {
this . mDialog= mDialog;
this . mLoginView= mLoginView;
if ( loginModel== null ) {
loginModel= new LoginModel ( ) ;
}
}
@Override
public void reqLogin ( String userName, String passWord) {
mDialog. showDialog ( "正在登陆中..." ) ;
loginModel. reqLogin ( userName, passWord, new LoginModel. LoginCallBack ( ) {
@Override
public void onSuccess ( String result) {
mDialog. dismissDialog ( ) ;
mLoginView. showData ( result) ;
}
@Override
public void onError ( String error) {
mDialog. dismissDialog ( ) ;
mLoginView. showData ( error) ;
}
} ) ;
}
}
推荐文章:https://blog.csdn.net/limonzet/article/details/53328315 源码Git___Demo------>>>>>>diudiudiu