在android中使用MVP模式
目标
使用MVP模式实现登录效果
MVP介绍
- M:Model负责存储、检索、操纵数据(有时也实现一个Model interface用来降低耦合);
- V:View interface需要View实现的接口,View通过View interface与Presenter进行交互,降低耦合,方便进行单元测试;
- P:Presenter作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。
Activity只负责初始化,负责绘制UI元素、与用户进行交互
使用MVP的优势
以往的MVC模式已经不能满足随着界面及其逻辑的复杂度不断提升的,Activity类的职责不断增加,以致变得庞大臃肿,当我们的项目达到一定程度的时候管理起来也显得非常吃力,然而MVP模式中 View,它负责UI元素的初始化,建立UI元素与Presenter的关联(Listener之类),同时自己也会处理一些简单的逻辑(复杂的逻辑交由 Presenter处理),所以对于单元测试、功能升级也是非常的方便。
模拟登录Demo
- m层,javaBean类:Info.java
- v层:LoginView.java、MainActivity.java
- p层:LoginPresenter.java、LoginPresenterCompl.java
/**
* Created by TangRen on 2016/8/15.
*/
public class Info {
private String name;
private String psw;
public Info(String name, String psw) {
super();
this.name = name;
this.psw = psw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
}
view接口
/**
* Created by TangRen on 2016/8/15.
*/
public interface LoginView {
public void onClearText();
public void onLoginResult(Boolean result, int code);
}
presenter接口
/**
* Created by TangRen on 2016/8/15.
*/
public interface LoginPresenter {
public void doLogin(EditText name, EditText psw);
public void clear();
}
presenter逻辑
/**
* Created by TangRen on 2016/8/15.
*/
public class LoginPresenterCompl implements LoginPresenter {
private LoginView loginView;
private Info info;
public LoginPresenterCompl(LoginView loginView) {
super();
this.loginView = loginView;
info = new Info("zhangsan", "1234");
}
@Override
public void doLogin(EditText name, EditText psw) {
Log.d("doLogin---",name.getText().toString());
boolean result = false;
int code = 0;
String userName = name.getText().toString();
String password = psw.getText().toString();
if (userName.equals("") || password.equals("")) {
result = false;
code = 0;
} else {
if (userName.equals(info.getName()) && password.equals(info.getPsw())) {
result = true;
code = 1;
} else {
result = false;
code = 2;
}
}
Log.d("code",code+"");
loginView.onLoginResult(result, code);
}
@Override
public void clear() {
loginView.onClearText();
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements LoginView {
private EditText user, psw;
private Button login, clear;
private LoginPresenter mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
user = (EditText) findViewById(R.id.user);
psw = (EditText) findViewById(R.id.psw);
login = (Button) findViewById(R.id.login);
clear = (Button) findViewById(R.id.clera);
mPresenter = new LoginPresenterCompl(this);
}
public void click(View view) {
switch (view.getId()) {
case R.id.login:
mPresenter.doLogin(user, psw);
break;
case R.id.clera:
mPresenter.clear();
break;
default:
break;
}
}
@Override
public void onClearText() {
user.setText("");
psw.setText("");
}
@Override
public void onLoginResult(Boolean result, int code) {
if (!result&&code == 0)
Snackbar.make(user, "User or Password not empt!", Snackbar.LENGTH_SHORT).show();
else if (code == 1)
Snackbar.make(user, "Login success!", Snackbar.LENGTH_SHORT).show();
else
Snackbar.make(user, "Login Fail!", Snackbar.LENGTH_SHORT).show();
}
}