MVP模型

MVP模型通常要建立三個文件夾:View、Interactor(Model)、Presenteride

每一個部分都有其接口和實現類,就是爲了方便回調post

這裏作一個登錄界面爲例子:ui

 接口:this

InteractorInterface:
public interface LoginInteractor { void login(String userName,String password,OnLoginFinishedListener listener); }
PresenterInterface
public interface LoginPresenter { void validateCredentials(String userName,String password); void onDestroy(); }
public interface OnLoginFinishedListener { void onUserNameError(); void onPasswordError(); void onSuccess(); }
ViewInterface
public interface LoginView { void setUserNameError(); void setPasswordError(); void navigateToHome(); }

實現類:spa

InteractorImpl
public class LoginInteractorImpl implements LoginInteractor { @Override public void login(final String userName, final String password, final OnLoginFinishedListener listener) { new Handler().postDelayed(new Runnable() { @Override public void run() { Log.i("ZhiXueHui", "LoginInteractorImpl-login"); boolean error = false; if(TextUtils.isEmpty(userName)){ listener.onUserNameError(); error = true; } if(TextUtils.isEmpty(password)){ listener.onPasswordError(); error = true; } if(!error){ listener.onSuccess(); } } },2000); } }
PresenterImpl
public class LoginPresenterImpl implements LoginPresenter,OnLoginFinishedListener { 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) { Log.i("ZhiXueHui", "LoginPresenterImpl-validateCredentials"); loginInteractor.login(userName,password,this); } @Override public void onDestroy() { Log.i("ZhiXueHui", "LoginPresenterImpl-onDestroy"); loginView = null; } @Override public void onUserNameError() { Log.i("ZhiXueHui", "LoginPresenterImpl-onUserNameError"); if(loginView!=null){ loginView.setUserNameError(); } } @Override public void onPasswordError() { Log.i("ZhiXueHui", "LoginPresenterImpl-onPasswordError"); if(loginView!=null){ loginView.setPasswordError(); } } @Override public void onSuccess() { Log.i("ZhiXueHui", "LoginPresenterImpl-onSuccess"); if(loginView!=null){ loginView.navigateToHome(); } } }
ViewActivity
public class LoginActivity extends Activity implements LoginView, View.OnClickListener { private EditText userName; private EditText password; private Button button; private LoginPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Log.i("ZhiXueHui", "LoginActivity-onCreate"); userName = (EditText) findViewById(R.id.userName); password = (EditText) findViewById(R.id.password); button = (Button) findViewById(R.id.button); button.setOnClickListener(this); presenter = new LoginPresenterImpl(this); } @Override public void setUserNameError() { Log.i("ZhiXueHui", "LoginActivity-setUserNameError"); userName.setError(getString(R.string.username_error)); } @Override public void setPasswordError() { Log.i("ZhiXueHui", "LoginActivity-setPasswordError"); password.setError(getString(R.string.password_error)); } @Override public void navigateToHome() { Log.i("ZhiXueHui", "LoginActivity-navigateToHome"); startActivity(new Intent(this, MainActivity.class)); finish(); } @Override public void onClick(View v) { Log.i("ZhiXueHui", "LoginActivity-onClick"); presenter.validateCredentials(userName.getText().toString(),password.getText().toString()); } }

流程圖:3d

流程解釋:code

第一步:View的實現類調用Presenter的接口blog

第二步:Presenter的實現類調用Model的接口接口

第三步:Model的實現類回調Presenter的接口get

第四步:Presenter的實現類回調View的接口

一個循環下來,正好是View->Presenter->Model->Presenter->View。

其中要注意的是,Presenter做爲中轉,分別對接View的實現類和Model的實現類,因此必需要創建兩個接口

相關文章
相關標籤/搜索