一、什么是MVP
在之前的开发中,采用的是android本身的MVC模式,然而随着业务逻辑的增加,软件的复杂度变高,MVC模式使得view层的逻辑代码变多,代码可读性变差。而MVP模式将业务逻辑代码放入presenter层。而且MVP模式使得view层和model层没有直接交互,提高了数据安全性。
下面看看在MVP模式里通常包含的4个要素:
(1)View:负责绘制UI元素、与用户进行交互(在Android中体现为Activity,fragment);
(2)View interface:需要View实现的接口,View通过View interface与Presenter进行交互,降低 耦合,方便进行单元测试;
(3)Model:负责存储、检索、操纵数据;
(4)Presenter:作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。
UML类图:
MVP模式UML类图
二、以登录为例实现MVP模式
view interface
<pre>
public interface LoginView {
void showLoading();
void showLoginFail(String msg);
void loginSuccess();
}
</pre>
view层继承view interface实现3个方法
<pre>
public class Activity_Login extends Activity implements LoginView,View.OnClickListener{
public ILoginPresenter presenter;
public EditText et_account_login;
public EditText et_password_login;
public Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
et_account_login=(EditText)findViewById(R.id.Account_login);
et_password_login=(EditText)findViewById(R.id.Password_login);
btn_login=(Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
presenter=new LoginPresenterImpl(this);
}
@Override
public void showLoading() {
}
@Override
public void showLoginFail(final String msg) {
et_account_login.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
Log.e("tag","failed");
}
});
}
@Override
public void loginSuccess() {
Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
Log.e("tag","success");
SPUtils.put(getApplicationContext(),SPUtils.SP_USERNAME,et_account_login.getText().toString());
SPUtils.put(getApplicationContext(),SPUtils.SP_PASSWORD,et_password_login.getText().toString());
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_login: String username=et_account_login.getText().toString();
String password=et_password_login.getText().toString();
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
Toast.makeText(this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
}else{
presenter.login(username,password);
}
break;
}
}
}
</pre>
model层
<pre>
public class LoginModel {
public final static int CODE_LOGIN_SUCCESS=1;
public final static int CODE_LOGIN_PASSWORD_WRONG=-2;
public final static int CODE_LOGIN_USER_WRONG=-1;
private RequestBody SetRequestBody(String username,String password){
RequestBody body=null;
okhttp3.FormBody.Builder formEncodingBuilder=new okhttp3.FormBody.Builder();
formEncodingBuilder.add(AppConstants.ParamKey.USERNAME,username);
formEncodingBuilder.add(AppConstants.ParamKey.PASSWORD,password);
body=formEncodingBuilder.build();
return body;
}
public Request LoginRequest(String username,String password){
Request request=new Request.Builder().url(AppConstants.RequestPath.LOGIN).post(SetRequestBody(username,password)).build();
return request;
}
</pre>
presenter层
<pre>
public interface ILoginPresenter {
void login(String username, String password);
}
</pre>
<pre>
public class LoginPresenterImpl implements ILoginPresenter{
private LoginView view;
private LoginModel model;
public LoginPresenterImpl(LoginView view){
this.view=view;
model=new LoginModel();
}
@Override
public void login(final String username, final String password) {
Log.e("tag","login");
view.showLoading();
Call call= App.getInstance().okHttpClient.newCall(model.LoginRequest(username,password));
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("tag","connectfailed");
view.showLoginFail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson=new Gson();
LoginState state=gson.fromJson(response.body().string(),LoginState.class);
if(state.getResult()==LoginModel.CODE_LOGIN_SUCCESS){
Log.e("tag","loginsuccess");
App.getInstance().user.setUsername(username);
App.getInstance().user.setPassword(password);
view.loginSuccess();
}else{
Log.e("tag","loginfailed");
view.showLoginFail(state.getResult()+"");
}
}
});
}
}
</pre>
写文档好难,还是先只是把代码贴出来吧