mvp模式android写法,Android开发学习--MVP模式入门(示例代码)

1、模型与视图完全分离,我们可以修改视图而不影响模型

2、可以更高效地使用模型,因为所有的交互都发生在一个地方——Presenter内部

3、我们可以将一个Presenter用于多个视图,而不需要改变Presenter的逻辑。这个特性非常的有用,因为视图的变化总是比模型的变化频繁。

4、如果我们把逻辑放在Presenter中,那么我们就可以脱离用户接口来测试这些逻辑(单元测试)

下面示例一个小案例:

项目架构:

首先bean层:

User.java

public classUser {privateString username;privateString password;publicString getUsername() {returnusername;

}publicString getPassword() {returnpassword;

}public voidsetUsername(String username) {this.username =username;

}public voidsetPassword(String password) {this.password =password;

}

}

MainActivity.java

public class MainActivity extends AppCompatActivity implementsIUserLoginView{privateEditText musername;privateEditText mpasssword;privateButton mlogin;privateButton mclear;privateProgressBar mpb;private UserLoginPresenter mUserLoginPresenter = new UserLoginPresenter(this);

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}private voidinitView() {

musername=(EditText) findViewById(R.id.username);

mpasssword=(EditText) findViewById(R.id.password);

mlogin=(Button)findViewById(R.id.login);

mclear=(Button)findViewById(R.id.clear);

mpb=(ProgressBar) findViewById(R.id.pb);

mlogin.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View view) {

mUserLoginPresenter.login();

}

});

mclear.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View view) {

mUserLoginPresenter.clear();

}

});

}

@OverridepublicString getUsername() {returnmusername.getText().toString();

}

@OverridepublicString getPassword() {returnmpasssword.getText().toString();

}

@Overridepublic voidclearUsername() {

musername.setText("");

}

@Overridepublic voidclearPassword() {

mpasssword.setText("");

}

@Overridepublic voidshowLoading() {

mpb.setVisibility(View.VISIBLE);

}

@Overridepublic voidhideLoading() {

mpb.setVisibility(View.GONE);

}

@Overridepublic voidtoMainActivity(User user) {

Toast.makeText(this, user.getUsername() +

" login success , to MainActivity", Toast.LENGTH_SHORT).show();

}

@Overridepublic voidshowFailError() {

Toast.makeText(this,"login failed", Toast.LENGTH_SHORT).show();

}

}

IUserLoginView.java

public interfaceIUserLoginView {//操作的目的

String getUsername();

String getPassword();voidclearUsername();voidclearPassword();//友好的交互

voidshowLoading();voidhideLoading();//操作的结果,对应的反馈

voidtoMainActivity(User user);voidshowFailError();

}

UserLoginPresenter.java

//Model和View的桥梁 login clear

public classUserLoginPresenter {privateIUserLoginView userLoginView;privateIUserBiz userBiz;private Handler mHandler = newHandler();publicUserLoginPresenter(IUserLoginView userLoginView){this.userLoginView =userLoginView;this.userBiz = newUserBiz();

}public voidlogin()

{

userLoginView.showLoading();

userBiz.login(userLoginView.getUsername(), userLoginView.getPassword(),newOnLoginListener()

{

@Overridepublic void loginsuccess(finalUser user)

{//需要在UI线程执行

mHandler.post(newRunnable()

{

@Overridepublic voidrun()

{

userLoginView.toMainActivity(user);

userLoginView.hideLoading();

}

});

}

@Overridepublic voidloginfail()

{//需要在UI线程执行

mHandler.post(newRunnable()

{

@Overridepublic voidrun()

{

userLoginView.showFailError();

userLoginView.hideLoading();

}

});

}

});

}public voidclear()

{

userLoginView.clearUsername();

userLoginView.clearPassword();

}

}

IUserBiz.java

public interfaceIUserBiz {public voidlogin(String username,String password,OnLoginListener loginListener);

}

UserBiz.java

public class UserBiz implementsIUserBiz {

@Overridepublic void login(final String username,final String password, finalOnLoginListener loginListener) {newThread(){

@Overridepublic voidrun() {try{

Thread.sleep(2000);

}catch(InterruptedException e)

{

e.printStackTrace();

}if("name".equals(username)&&"pwd".equals(password)){

User user= newUser();

user.setUsername(username);

user.setPassword(password);

loginListener.loginsuccess(user);

}else{

loginListener.loginfail();

}

}

}.start();

}

}

OnLoginListener.java

public interfaceOnLoginListener {voidloginsuccess(User user);voidloginfail();

}

你可能感兴趣的:(mvp模式android写法)