【Android】设计模式与应用 -「建造者模式」封装网络请求

    技术2022-07-10  97

    Android设计模式详细介绍参阅 Android的设计模式-建造者模式

    首先,网络结构封装的目录结构为 建造者模式分为四种角色: Product(产品类) RestService.java

    package com.zjn.quickstart.latte_core.net; import java.util.Map; import okhttp3.MultipartBody; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.DELETE; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.PUT; import retrofit2.http.Part; import retrofit2.http.QueryMap; import retrofit2.http.Streaming; import retrofit2.http.Url; public interface RestService { @GET Call<String> get(@Url String url, @QueryMap Map<String,Object> params); @FormUrlEncoded @POST Call<String> post(@Url String url, @FieldMap Map<String,Object> params); @FormUrlEncoded @PUT Call<String> put(@Url String url, @FieldMap Map<String,Object> params); @DELETE Call<String> delete(@Url String url, @QueryMap Map<String,Object> params); @Streaming @GET Call<ResponseBody> download(@Url String url, @QueryMap Map<String,Object> params); @Multipart @POST Call<String> upload(@Url String url, @Part MultipartBody.Part file); }

    Builder(抽象建造者) RestClientBuilder.java

    package com.zjn.quickstart.latte_core.net; import com.zjn.quickstart.latte_core.net.callback.IError; import com.zjn.quickstart.latte_core.net.callback.IFailure; import com.zjn.quickstart.latte_core.net.callback.IRequest; import com.zjn.quickstart.latte_core.net.callback.ISuccess; import java.util.Map; import java.util.WeakHashMap; import okhttp3.MediaType; import okhttp3.RequestBody; public class RestClientBuilder { private String mUrl; private static final Map<String, Object> PARAMS = RestCreator.getParams(); private IRequest mIRequest; private ISuccess mISuccess; private IFailure mIFailure; private IError mIError; private RequestBody mBody; RestClientBuilder() { } public final RestClientBuilder url(String url){ this.mUrl = url; return this; } public final RestClientBuilder params(Map<String,Object> params){ PARAMS.putAll(params); return this; } public final RestClientBuilder params(String key,Object value){ PARAMS.put(key,value); return this; } public final RestClientBuilder raw(String raw){ this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),raw); return this; } public final RestClientBuilder onRequest(IRequest iRequest){ this.mIRequest = iRequest; return this; } public final RestClientBuilder success(ISuccess iSuccess){ this.mISuccess = iSuccess; return this; } public final RestClientBuilder failure(IFailure iFailure){ this.mIFailure = iFailure; return this; } public final RestClientBuilder error(IError iError){ this.mIError = iError; return this; } public final RestClient build() { return new RestClient(mUrl, PARAMS, mIRequest, mISuccess, mIFailure, mIError, mBody); } }

    ConcreteBuilder(具体建造者) RestClient.java

    package com.zjn.quickstart.latte_core.net; import com.zjn.quickstart.latte_core.net.callback.IError; import com.zjn.quickstart.latte_core.net.callback.IFailure; import com.zjn.quickstart.latte_core.net.callback.IRequest; import com.zjn.quickstart.latte_core.net.callback.ISuccess; import com.zjn.quickstart.latte_core.net.callback.RequestCallbacks; import java.util.Map; import java.util.WeakHashMap; import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.Callback; public class RestClient { private final String URL; private static final WeakHashMap<String, Object> PARAMS = RestCreator.getParams(); private final IRequest REQUEST; private final ISuccess SUCCESS; private final IFailure FAILURE; private final IError ERROR; private final RequestBody BODY; public RestClient(String url, Map<String, Object> params, IRequest request, ISuccess success, IFailure failure, IError error, RequestBody body) { URL = url; PARAMS.putAll(PARAMS); REQUEST = request; SUCCESS = success; FAILURE = failure; ERROR = error; BODY = body; } //测试方法 public static RestClientBuilder builder(){ return new RestClientBuilder(); } private void request(HttpMethod method){ final RestService service = RestCreator.getRestService(); Call<String> call = null; if(REQUEST != null){ REQUEST.onRequestStart(); } switch (method){ case GET: call = service.get(URL,PARAMS); break; case POST: call = service.post(URL,PARAMS); break; case PUT: call = service.put(URL,PARAMS); break; case DELETE: call = service.delete(URL,PARAMS); break; default: break; } if(call!=null){ call.enqueue(getRequestCallback()); } } private Callback<String> getRequestCallback(){ return new RequestCallbacks( REQUEST, SUCCESS, FAILURE, ERROR ); } public final void get(){ request(HttpMethod.GET); } public final void post(){ request(HttpMethod.POST); } public final void put(){ request(HttpMethod.PUT); } public final void delete(){ request(HttpMethod.DELETE); } }

    Director(指挥者类) RestCreator.java

    package com.zjn.quickstart.latte_core.net; import com.zjn.quickstart.latte_core.app.ConfigType; import com.zjn.quickstart.latte_core.app.Latte; import java.util.WeakHashMap; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.scalars.ScalarsConverterFactory;; //java并发编程推荐的单例模式-holder public class RestCreator { private static final class ParamsHolder{ public static final WeakHashMap<String,Object> PARAMS = new WeakHashMap<>(); } public static WeakHashMap<String,Object> getParams(){ return ParamsHolder.PARAMS; } public static RestService getRestService() { return RestServiceHolder.REST_SERVICE; } private static final class RetrofitHolder { private static final String BASE_URL = (String) Latte.getConfiguration().get(ConfigType.API_HOST.name()); //指挥者类:用于组装 private static final Retrofit RETROFIT_CLIENT = new Retrofit.Builder() .baseUrl(BASE_URL) .client(OKHttpHolder.OK_HTTP_CLIENT) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } private static final class OKHttpHolder{ private static final int TIME_OUT = 60; private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder() .connectTimeout(TIME_OUT, TimeUnit.SECONDS) .build(); } private static final class RestServiceHolder{ private static final RestService REST_SERVICE = RetrofitHolder.RETROFIT_CLIENT.create(RestService.class); } }
    Processed: 0.010, SQL: 9