MVP解析,mvp模式解析實踐

來源:互聯網
上載者:User

MVP解析,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 {}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.