标签:
MVP是什么?或许在之前更多的人知道的是MVC这个模式(Model View Controller),然而MVP与MVC最不同的一点是M与V是不直接
关联的也是就Model与View不存在直接关系,这两者之间间隔着的是Presenter层。个人感觉这是一种很棒的设计,让代码能够实现充分的解耦。
那么我们还是没有讲到MVP是什么~~~不要急,我会用最简单的方式来叙述,这样好理解~
M(Model):为UI层提供数据,或者是保存UI层的数据;
V(View) :单纯的数据展示,响应用户的操作并且都转发给Presenter来做具体的处理;
P(Presenter):逻辑控制层,从Model处取数据,运算和转化,最后用View来展示;并处理View传过来的用户事件,并做处理;
MVP是一个方法论的东西,也就是没有任何固定的具体的实现形式,只要能够把View跟Model解除联系,把逻辑都放在Presenter中,那么就能算
得上是MVP,一些具体的实践的指导性原则:
接下来我们使用一个登陆的例子来说明:
登陆View的接口:
public interface ILoginView { void clearEditText(); void showProgress(); void hideProgress(); void setUsernameError(); void setPasswordError(); String getUsername(); String getPassword(); void loginSuccess(); }
public interface ILoginPresenter { void doLogin(String username, String password); void clear(); void onDestroy(); }
public class LoginPresenter implements ILoginPresenter { private ILoginView mLoginView; private User mUser; public LoginPresenter(ILoginView loginView) { this.mLoginView = loginView; initUser(); } private void initUser() { mUser = new User(mLoginView.getUsername(), mLoginView.getPassword()); } @Override public void doLogin(String username, String password) { mLoginView.showProgress(); new Handler().postDelayed(new Runnable() { @Override public void run() { mLoginView.hideProgress(); int code = mUser.checkUserValidity(mLoginView.getUsername(), mLoginView.getPassword()); if (code == -1) { mLoginView.setPasswordError(); } else if (code == 0) { mLoginView.loginSuccess(); } } }, 2000); } @Override public void clear() { mLoginView.clearEditText(); } @Override public void onDestroy() { mLoginView = null; } }
定义Model:
public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(String username, String password) { this.username = username; this.password = password; } public int checkUserValidity(String username, String password) { if (username == null || password == null || username.isEmpty() || password.isEmpty()) { return -1; } return 0; } }
在Activity中实现view:
public class LoginActivity extends AppCompatActivity implements ILoginView, View.OnClickListener { private ILoginPresenter mLoginPresenter; @Bind(R.id.et_username) EditText etUsername; @Bind(R.id.et_passwrod) EditText etPasswrod; @Bind(R.id.bt_enter) Button btEnter; @Bind(R.id.bt_clear) Button btClear; @Bind(R.id.progress) ProgressBar progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); mLoginPresenter = new LoginPresenter(this); btEnter.setOnClickListener(this); btClear.setOnClickListener(this); } @Override public void clearEditText() { etPasswrod.setText(""); etUsername.setText(""); } @Override public void showProgress() { progress.setVisibility(View.VISIBLE); } @Override public void hideProgress() { progress.setVisibility(View.GONE); } @Override public void setUsernameError() { etUsername.setError("username error"); } @Override public void setPasswordError() { etPasswrod.setError("password error"); } @Override public String getUsername() { return etUsername.getText().toString(); } @Override public String getPassword() { return etPasswrod.getText().toString(); } @Override public void loginSuccess() { //start act Main Toast.makeText(this, "login success", Toast.LENGTH_SHORT); finish(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_clear: mLoginPresenter.clear(); break; case R.id.bt_enter: mLoginPresenter.doLogin(etUsername.getText().toString(), etPasswrod.getText().toString()); break; } } @Override protected void onDestroy() { mLoginPresenter.onDestroy(); super.onDestroy(); } }
标签:
原文地址:http://www.cnblogs.com/liuzhenlong/p/5229487.html