Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> MVP解析,mvp模式解析實踐

MVP解析,mvp模式解析實踐

編輯:關於android開發

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 {
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved