MVCMVPMVVM模式对比总结用户登录例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MVCMVPMVVM模式对比总结用户登录例子相关的知识,希望对你有一定的参考价值。
前言说明
在实战项目及学习中来总结一下android端项目构架
包括MVC、MVP、MVVM,主要针对移动Android端
该篇以Android平台用户登录为例子,用代码来描述构架模式
代码主要作理解,所以可能不完整,因为去掉一些精简方便说明
目录
4. 用户登录例子
公共代码
NetHelper网络通信类
使用Retrofit+RxJava技术
NetHelper { private static String baseUrl = "http://xxx/"; private static Retrofit retrofit = null; private static OkHttpClient okHttpClient = null; private static NetService netService; static { OkHttpClient.Builder okBuilder = OkHttpClient.Builder(); okBuilder.addInterceptor(new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BODY)); okHttpClient = okBuilder.build(); Retrofit.Builder builder = new Retrofit.Builder(); retrofit = builder.baseUrl(baseUrl) .client(sOkHttpClient) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); netService = sRetrofit.create(NetService.class); } public static NetService getNetService() { reutrn netService; } public interface NetService { @POST("login") Observable<String> login(@Query("username") username, @Query("userpass") userpass); } }
用户业务UserBiz( model层)
UserBiz
{
Observable<String> login(String username, String userpass) {
return NetHelper.getNetService.login(username, userpass);
}
}
MVC例子代码
布局文件
<Layout> <TextView id="username"/> <TextView id="userpass"/> <Button id="button" /> </Layout>
登录界面LoginActivity( view层)
LoginActivity extends Activity { String username; String userpass; void onCreate(Bundle bdle) { setContentView(login_layout); username = findById(username); userpass = findById(userpass); findById(button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); } void login() { new UserManager() .login(username, userpass) .subscribe(new Action1<String>() { @Override public void call(String loginResultString) { if(loginResultString) { Toast.makeText(getApplicationContext(), username() + "--登录成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), username() + "--登录失败", Toast.LENGTH_SHORT).show(); } } }; } }
用户管理UserManager( controller层)
UserManager { Observable<String> login(String username, String userpass) { return new UserBiz().login(username, userpass) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } }
用户业务UserBiz( model层)
查看公共代码
MVP例子代码
布局文件
<Layout> <TextView id="username"/> <TextView id="userpass"/> <Button id="button" /> </Layout>
登录界面LoginActivity( view层)
LoginActivity extends Activity impl IUserView { String username; String userpass; void onCreate(Bundle bdle) { setContentView(login_layout); username = findById(username); userpass = findById(userpass); findById(button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new UserPresenter(this).login() } }); } @Override String getUserName() { return username; } @Override String getPassword() { return userpass; } @Override void showSuccess() { Toast.makeText(getApplicationContext(), username() + "--登录成功", Toast.LENGTH_SHORT).show(); } @Override void showFailure() { Toast.makeText(getApplicationContext(), username() + "--登录失败", Toast.LENGTH_SHORT).show(); } }
用户视图接口IUserView( view层)
interface IUserView { String getUserName(); String getUserPass(); void showSuccess(); void showFailure(); }
用户调配UserPresenter( presenter层)
UserPresenter { IUserView userView; UserPresenter(IUserView userView) { this.userView = userView; } void login() { new UserBiz().login(userView.getUserName(), userView.getUserPass()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<String>() { @Override public void call(String loginResultString) { if(loginResultString) { userView.showSuccess(); } else { userView.showFailure(); } } }; } }
用户业务UserBiz( model层)
查看公共代码
MVVM例子代码
布局文件
<layout> <data> <name="userViewModel" type="com.test.viewModel.UserViewModel" /> </data> <layout> <TextView id="username"/> <TextView id="userpass"/> <Button id="button" onclick="@{userViewModel.login}" /> </layout> </layout>
登录界面LoginActivity( view层)
LoginActivity extends Activity { void onCreate(Bundle bdle) { MainBinding binding = DataBindingUtil.setContentView(this, login_layout); binding.setViewModel(new UserViewModel(binding, this)); } }
用户视图模型UserViewModel( viewModel层)
UserViewModel { String username; String userpass; Context context; UserViewModel(MainBinding mainBinding, Context context) { this.context = context; username = mainBinding.username.getText().toString.trim(); userpass = mainBinding.userpass.getText().toString.trim(); } void login() { new UserBiz().login(username, userpass) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<String>() { @Override public void call(String loginResultString) { if(loginResultString) { Toast.makeText(context, username + "--登录成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, username + "--登录失败", Toast.LENGTH_SHORT).show(); } } }; } }
用户业务UserBiz( model层)
查看公共代码
以上是关于MVCMVPMVVM模式对比总结用户登录例子的主要内容,如果未能解决你的问题,请参考以下文章