1. 概述
MVP,即是Model,View,Presenter架构模式.看起来类似MVC,其实不然.从上图能看到Model层和View层没有相连接,完全解耦.
2. 分层
- M层:模型层(Model),此层和MVC中的M层作用类似.主要是实体类,数据库,网络等存在的层面
- V层:视图层(View),在MVC中V层只包含XML文件,而MVP中V层包含XML,Activity和Fragment三者.理论上V层不涉及任何逻辑,只负责界面的改变,尽量把逻辑处理放到M层.
- P层:通知层(Presenter),P层的主要作用就是连接V层和M层,起到一个通知传递数据的作用.
3. 原理
用户触碰界面触发事件,View层把事件通知Presenter层,Presenter层通知Model层处理这个事件,Model层处理后把结果发送到Presenter层,Presenter层再通知View层,最后View层做出改变.这是一整套流程.
4. MVP架构优缺点
- 缺点
MVP中接口过多.
每一个功能,相比于MVC要多写好几个文件.
如果某一个界面中需要请求多个服务器接口,这个界面文件中会实现很多的回调接口,导致代码繁杂.
如果更改了数据源和请求中参数,会导致更多的代码修改.
额外的代码复杂度及学习成本.
- 优点
模块职责划分明显,层次清晰,接口功能清晰.
Model层和View层分离,解耦.修改View而不影响Model.
功能复用度高,方便.一个Presenter可以复用于多个View,而不用更改Presenter的逻辑.
有利于测试驱动开发,以前的Android开发是难以进行单元测试.
如果后台接口还未写好,但已知返回数据类型的情况下,完全可以写出此接口完整的功能.
5. MVP简单案例
①.布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入账号"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/tv_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"
app:layout_constraintTop_toBottomOf="@id/tv_name" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv_pass"
android:text="登录" />
</androidx.constraintlayout.widget.ConstraintLayout>
②. 页面
public class MainActivity extends AppCompatActivity {
private Button btn;
private TextView tv_item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn = (Button) findViewById(R.id.btn);
tv_item = (TextView) findViewById(R.id.tv_item);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
break;
}
}
}
④. Api
public interface ApiService {
String baseUrl="https://www.wanandroid.com/user/";
@POST("login")
@FormUrlEncoded
Observable<MyBean> post(@Field("username") String username, @Field("password") String password);
}
⑤. Model
public interface LoginModel {
void login(LoginCallback loginCallback,String name,String pass);
}
public class ImpLoginModel implements LoginModel{
@Override
public void login(final LoginCallback loginCallback, String name, String pass) {
final Retrofit retrofit= new Retrofit.Builder()
.baseUrl(ApiService.baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
Observable<MyBean> post = apiService.post(name, pass);
post.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MyBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(MyBean myBean) {
int errorCode = myBean.getErrorCode();
if(errorCode==0){
loginCallback.onSuccess(myBean);
}else {
loginCallback.onFail(myBean.getErrorMsg());
}
}
@Override
public void onError(Throwable e) {
loginCallback.onFail("网络错误");
}
@Override
public void onComplete() {
}
});
}
}
⑥. CallBack
public interface LoginCallback {
void onSuccess(MyBean myBean);
void onFail(String error);
}
⑥. Presenter
public interface LoginPresenter {
void loge(String name,String pass);
}
public class ImpLoginPresenter implements LoginPresenter, LoginCallback {
private LoginView loginView;
private ImpLoginModel impLoginModel;
public ImpLoginPresenter(LoginView loginView) {
this.loginView = loginView;
impLoginModel=new ImpLoginModel();
}
@Override
public void loge(String name, String pass) {
if(TextUtils.isEmpty(name)){
loginView.onFail("输入账号");
}
if(TextUtils.isEmpty(pass)){
loginView.onFail("输入密码");
}
impLoginModel.login(this,name,pass);
}
@Override
public void onSuccess(MyBean myBean) {
loginView.onSuccess(myBean);
}
@Override
public void onFail(String error) {
loginView.onFail(error);
}
}
⑦. IView
public interface LoginView {
void onSuccess(MyBean myBean);
void onFail(String error);
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener, LoginView {
private EditText tv_name;
private EditText tv_pass;
private Button btn_login;
private ImpLoginPresenter impLoginPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
impLoginPresenter = new ImpLoginPresenter(this);
initView();
}
private void initView() {
tv_name = (EditText) findViewById(R.id.tv_name);
tv_pass = (EditText) findViewById(R.id.tv_pass);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
login();
break;
}
}
private void login() {
String name = tv_name.getText().toString();
String pass = tv_pass.getText().toString();
impLoginPresenter.loge(name,pass);
}
@Override
public void onSuccess(MyBean myBean) {
Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onFail(String error) {
Toast.makeText(this,error,Toast.LENGTH_SHORT).show();
}
}