你的位置:首页 > 操作系统

[操作系统]MVP解析


一套可以直接复制使用的MVP框架

通过对MVP设计模式学习,对MVP也有了一个初步的认识,以登录Login模块为例,封装MVP如下:

package com.example.administrator.frameapp.api;/** * 存放url的接口 * Created by Zyh on 2016/11/17. */public interface ApiUrl {  String IP="http://192.168.8.4/tp3/";  String BASEURL=IP+"api.php/Home/";}

package com.example.administrator.frameapp.api;import io.reactivex.Flowable;import retrofit2.http.Field;import retrofit2.http.FormUrlEncoded;import retrofit2.http.POST;/** * Created by Zyh on 2016/11/17. */public interface ApiService {  @FormUrlEncoded  @POST("login/login")  Flowable<ApiResult> login(@Field("name") String name, @Field("password") String password);}

package com.example.administrator.frameapp.api;/** * Created by Zyh on 2016/11/17. */public class ApiResult<T> {  private int code;  private String Msg;  private T data;  public int getCode() {    return code;  }  @Override  public String toString() {    return "ApiResult{" +        "code=" + code +        ", Msg='" + Msg + '\'' +        ", data=" + data +        '}';  }  public void setCode(int code) {    this.code = code;  }  public String getMsg() {    return Msg;  }  public void setMsg(String msg) {    Msg = msg;  }  public T getData() {    return data;  }  public void setData(T data) {    this.data = data;  }}

package com.example.administrator.frameapp.api;import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;import okhttp3.OkHttpClient;import okhttp3.logging.HttpLoggingInterceptor;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;/** * Created by Zyh on 2016/11/17. */public class Api {  private Retrofit mRetrofit;  public ApiService mApiservice;  private Api() {    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//请求的内容和响应的内容都存在这个系统的BODY中    OkHttpClient mOkHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();    mRetrofit = new Retrofit.Builder()        .client(mOkHttpClient)        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())        .addConverterFactory(GsonConverterFactory.create())        .baseUrl(ApiUrl.BASEURL)        .build();    mApiservice = mRetrofit.create(ApiService.class);  }  //静态内部类的单例模式:内部类决定了什么时候加载他就什么时候进行加载,  private static class SingleHolder {    private static final Api INSTANCE = new Api();  }  public static Api getInstance() {    return SingleHolder.INSTANCE;  }}

 

package com.example.administrator.frameapp.ui.base;/** * 创建base类是为了统一管理 * BasePresent是抽象类 * 将model和view关联起来 * Created by Zyh on 2016/11/17. */public abstract class BasePresent<M,V> {  public M mModel;  public V mView;  public void setVM(V v,M m){    //这个方法将LoginPresenter中方法中类型映射成具体的类型    this.mModel=m;    this.mView=v;  }}

package com.example.administrator.frameapp.ui.base;/** * Created by Zyh on 2016/11/17. */public interface BaseView {}

package com.example.administrator.frameapp.ui.base;/** * Created by Zyh on 2016/11/17. */public interface BaseModel {}