最近在项目中使用到了安卓开发MVP模式,用过之后才发现MVP的巨大优势,真正实现了层次之间的解耦。
特点
View层只负责向用户展示数据与接收用户输入;
Model负责数据处理,联系业务层、数据访问,网络请求等工作;
Presenter负责联系M层与V层,进行逻辑处理;
建议
加一个单独的bean层,包含实体;
每个层都使用接口,使代码更易更改、拓展
分包情况
bean + view + model + presenter
bean层包含了loginBean;
view层包含 loginView接口以及实现了接口的LoginActivity和LoginByPsdActivity;
presenter包含OnLoginListener接口和实现了接口的LoginPresenter;
model层包含LoginModel接口和实现了接口的LoginModelImpl。
代码
LoginView
public interface LoginView {
void moveToMain(LoginBean loginBean);
String getPhoneNumber();
String getEmail();
String getVerifyCode();
String getPassword();
String getLoginType();
void ShowError(String errorInfo);
void ShowNetTimeOut();
void ShowServerFailed();
}
LoginActivity(局部)
public class LoginActivity extends Activity implements LoginView,SendSMSView {
private LoginPresenter loginPresenter;
private SendSMSPresenter sendSMSPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//初始化控件
initView();
loginPresenter = new LoginPresenter(this);
sendSMSPresenter = new SendSMSPresenter(this);
}
public void moveToMain(LoginBean loginBean) {
Intent intent = new Intent();
intent.setClass(LoginActivity.this, MainActivity.class);
startActivity(intent);
URound.setUserId(loginBean.getPhoneNumber());
URound.setUserPassword(loginBean.getPassword());
URound.setmIMKit();
finish();
}
@Override
public void ShowNetTimeOut() {
}
@Override
public String getVerifyCode() {
return mEtVerifyCode.getText().toString();
}
@Override
public String getPassword() {
return null;
}
@Override
public String getPhoneNumber() {
return mEtPhone.getText().toString();
}
@Override
public String getEmail() {
return null;
}
@Override
public String getLoginType() {
return CommonParameters.LOGIN_TYPE_VERIFY_CODE;
}
@Override
public void SendSuccess() {
mBtnSendVerifyCode.setText("已发送");
}
@Override
public String getSendType() {
return CommonParameters.SMS_TYPE_REGISTER;
}
@Override
public void OnFailed(String errorInfo) {
}
@Override
public void ShowError(String errorInfo) {
Toast.makeText(this, errorInfo, Toast.LENGTH_SHORT).show();
}
@Override
public void ShowServerFailed() {
Toast.makeText(this,"服务器开小差啦,请稍后再试吧", Toast.LENGTH_SHORT).show();
}
}
}
LoginModel
public interface LoginModel {
void login(String phone, String email, String loginType, String verifyCode,
String password, final OnLoginListener onLoginListener);
}
LoginModelImpl:
public class LoginModelImpl implements LoginModel {
private LoginBean loginBean;
private OnLoginListener loginListener;
@Override
public void login(String phone, String email, String loginType,
String verifyCode, String password, OnLoginListener onLoginListener) {
loginListener = onLoginListener;
loginBean = new LoginBean();
loginBean.setApplication("login");
loginBean.setVersion(CommonParameters.VERSION);
loginBean.setClientFlag(CommonParameters.CLIENT_FLAG);
loginBean.setClientVersion(CommonParameters.CLIENT_VERSION);
loginBean.setPhoneNumber(phone);
loginBean.setEmail(email);
loginBean.setLoginType(loginType);
loginBean.setVerifyCode(verifyCode);
loginBean.setPassword(password);
new LoginTask().execute();
}
private class LoginTask extends AsyncTask<Void, Void, String> {
public LoginTask() {
super();
}
@Override
protected void onPostExecute(String result) {
Gson gson = new Gson();
if (HttpWrapper.HTTPSTATE_ERROR.equals(result)) {
//网络错误
} else if (HttpWrapper.HTTPSTATE_TIMEOUT.equals(result)) {
//链接超时
} else {
LoginBean loginBean = gson.fromJson(result, LoginBean.class);
if (loginBean != null) {
if (CommonParameters.SUCCESS_CODE.equals(loginBean.getCode())) {
//成功接收
loginListener.OnSuccess(loginBean);
} else if(CommonParameters.FAILED_CODE.equals(loginBean.getCode())){
//系统异常
loginListener.OnServerFailed();
}else {
loginListener.OnFailed(loginBean.getDesc());
}
} else {
//接收空数据??数据解析错误
}
}
super.onPostExecute(result);
}
@Override
protected String doInBackground(Void... voids) {
Gson gson = new Gson();
String request = gson.toJson(loginBean);
String response = HttpWrapper.SubmitHttpRequestPost(CommonParameters.LOGIN_URL, request);
return response;
}
}
}
OnLoginListener
public interface OnLoginListener {
void OnSuccess(LoginBean loginBean);
void OnDisConnect();
void OnTimeOut();
void OnServerFailed();
void OnFailed(String errorDesc);
}
LoginPresenter
public class LoginPresenter implements OnLoginListener {
private LoginModel loginModel;
private LoginView loginView;
public LoginPresenter(LoginView loginView) {
this.loginView = loginView;
loginModel = new LoginModelImpl();
}
public void login() {
loginModel.login(loginView.getPhoneNumber(), loginView.getEmail(),
loginView.getLoginType(), loginView.getVerifyCode(), loginView.getPassword(), this);
}
@Override
public void OnDisConnect() {
}
@Override
public void OnFailed(String errorDesc) {
loginView.ShowError(errorDesc);
}
@Override
public void OnServerFailed() {
loginView.ShowServerFailed();
}
@Override
public void OnSuccess(LoginBean loginBean) {
loginView.moveToMain(loginBean);
}
@Override
public void OnTimeOut() {
}
}
可见,MVP完全实现了模块间的解耦。