使用MVP模式来解耦activity中业务代码和界面代码。在activity中,将其中的业务抽象到presenter层;将其中的界面代码抽象到View层。
MVP模式:
一个软件被划分成三层,View层,Presenter层,Model层。
View层的职责是展示界面,界面绘制。
Presenter层的职责是,实现各种业务逻辑,业务逻辑的代码都放在这一层。
Model层的职责是数据的存储,修改,获取。
各层之间的交互:
View层和Presenter层之间的通信是双向的。
Presenter层和Model层之间的通信是双向的。
View层不与Model层发生交互。
MVP模式应用到Android app:
View层:activity,fragment,其中的界面展示,就是View层的内容。
Presenter层:点击一个按钮要执行的业务逻辑,则是由Presenter层来实现。也就是说,Presenter层抽象,提取出activity,fragment中的业务逻辑。这样就可以将业务逻辑代码与界面展示代码解耦掉。可以重用业务逻辑。
Model层:这一层,则是数据存取,数据修改层。配置信息,获取数据源的数据,获取服务器数据,获取数据库的数据,更新数据库的数据,这些实现代码都由这一层来提供。
MVP模式应用到Android app的一个例子分析:
Login这个模块,把View层和presenter层放在了一起:
package com.antonioleiva.mvpexample.app.Login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import com.antonioleiva.mvpexample.app.R;
import com.antonioleiva.mvpexample.app.main.MainActivity;
public class LoginActivity extends Activity implements LoginView, View.OnClickListener {
private ProgressBar progressBar;
private EditText username;
private EditText password;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progressBar = (ProgressBar) findViewById(R.id.progress);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
findViewById(R.id.button).setOnClickListener(this);
presenter = new LoginPresenterImpl(this);
}
@Override public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
}
@Override public void hideProgress() {
progressBar.setVisibility(View.GONE);
}
@Override public void setUsernameError() {
username.setError(getString(R.string.username_error));
}
@Override public void setPasswordError() {
password.setError(getString(R.string.password_error));
}
@Override public void navigateToHome() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
@Override public void onClick(View v) {
presenter.validateCredentials(username.getText().toString(), password.getText().toString());
}
}
this.loginView = loginView;
this.loginInteractor = new LoginInteractorImpl();
}
loginView.setUsernameError();
loginView.hideProgress();
}
loginView.setPasswordError();
loginView.hideProgress();
}
loginView.navigateToHome();
}
public void validateCredentials(String username, String password);
}
private LoginView loginView;
private LoginInteractor loginInteractor;
public LoginPresenterImpl(LoginView loginView) {
this.loginView = loginView;
this.loginInteractor = new LoginInteractorImpl();
}
@Override public void validateCredentials(String username, String password) {
loginView.showProgress();
loginInteractor.login(username, password, this);
}
@Override public void onUsernameError() {
loginView.setUsernameError();
loginView.hideProgress();
}
@Override public void onPasswordError() {
loginView.setPasswordError();
loginView.hideProgress();
}
@Override public void onSuccess() {
loginView.navigateToHome();
}
}
public void onUsernameError();
public void onPasswordError();
public void onSuccess();
}
@Override
public void login(final String username, final String password, final OnLoginFinishedListener listener) {
// Mock login. I'm creating a handler to delay the answer a couple of seconds
new Handler().postDelayed(new Runnable() {
@Override public void run() {
boolean error = false;
if (TextUtils.isEmpty(username)){
listener.onUsernameError();
error = true;
}
if (TextUtils.isEmpty(password)){
listener.onPasswordError();
error = true;
}
if (!error){
listener.onSuccess();
}
}
}, 2000);
}
}
loginView.showProgress();
loginInteractor.login(username, password, this);
}
public void showProgress();
public void hideProgress();
public void setUsernameError();
public void setPasswordError();
public void navigateToHome();
}
public void showProgress();
public void hideProgress();
public void setItems(List<String> items);
public void showMessage(String message);
}
private ListView listView;
private ProgressBar progressBar;
private MainPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
progressBar = (ProgressBar) findViewById(R.id.progress);
presenter = new MainPresenterImpl(this);
}
@Override protected void onResume() {
super.onResume();
presenter.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
listView.setVisibility(View.INVISIBLE);
}
@Override public void hideProgress() {
progressBar.setVisibility(View.INVISIBLE);
listView.setVisibility(View.VISIBLE);
}
@Override public void setItems(List<String> items) {
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
@Override public void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
presenter.onItemClicked(position);
}
}
public void onResume();
public void onItemClicked(int position);
}
private MainView mainView;
private FindItemsInteractor findItemsInteractor;
public MainPresenterImpl(MainView mainView) {
this.mainView = mainView;
findItemsInteractor = new FindItemsInteractorImpl();
}
@Override public void onResume() {
mainView.showProgress();
findItemsInteractor.findItems(this);
}
@Override public void onItemClicked(int position) {
mainView.showMessage(String.format("Position %d clicked", position + 1));
}
@Override public void onFinished(List<String> items) {
mainView.setItems(items);
mainView.hideProgress();
}
}