泛型模板化設計DEMO
1. 定義Result泛型類
package com.example.core.mydemo.java.fanxing; public class Result<T> { T response; public T getResponse() { return response; } public void setResponse(T response) { this.response = response; } }
2.定義了程式的主要實現流程
package com.example.core.mydemo.java.fanxing; public abstract class BaseService<T> { /** * 定義了程式的主要實現流程-骨架 * @return */ public Result handle(BaseRequest request) throws Exception{ Result result = new Result(); method1(); // result.setResponse(method2()); /** * request與response物件的繫結關係 */ Class<T> responseClass = request.getResponseClass(); T rspt = null; rspt = responseClass.newInstance(); rspt = method2(); result.setResponse(rspt); return result; } /** * 返回值-泛型,根據不同的業務返回不同的響應型別 * @return */ private T method2() { T response = initResponse();//獲取子類初始化的響應例項 System.out.println("BaseService method2"); return response; } /** * 公共處理業務 */ private void method1() { System.out.println("BaseService method1"); } /** * 響應型別-泛型,提供出去給具體實現類進行初始化 * @return */ protected abstract T initResponse(); }
3.請求基類和返回基類
package com.example.core.mydemo.java.fanxing; public interface BaseRequest<T extends BaseResponse> { Class<T> getResponseClass(); } package com.example.core.mydemo.java.fanxing; public class BaseResponse { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.第一個實現類
package com.example.core.mydemo.java.fanxing; public class ChinaRequest implements BaseRequest<ChinaResponse>{ @Override public Class<ChinaResponse> getResponseClass() { return ChinaResponse.class; } } package com.example.core.mydemo.java.fanxing; import java.util.List; public class ChinaResponse extends BaseResponse{ List<String> province; public List<String> getProvince() { return province; } public void setProvince(List<String> province) { this.province = province; } } package com.example.core.mydemo.java.fanxing; import java.util.Arrays; public class ChinaServiceImpl extends BaseService<ChinaResponse>{ /** * 實現抽象方法,初始化返回值型別 * @return */ @Override protected ChinaResponse initResponse() { ChinaResponse chinaResponse = new ChinaResponse(); chinaResponse.setName("中國"); chinaResponse.setProvince(Arrays.asList("湖南","山東")); return chinaResponse; } }
5.第二個實現類
package com.example.core.mydemo.java.fanxing; public class AmericaRequest implements BaseRequest<AmericaResponse>{ @Override public Class<AmericaResponse> getResponseClass() { return AmericaResponse.class; } } package com.example.core.mydemo.java.fanxing; import java.util.List; public class AmericaResponse extends BaseResponse{ List<String> state; public List<String> getState() { return state; } public void setState(List<String> state) { this.state = state; } } package com.example.core.mydemo.java.fanxing; import java.util.Arrays; public class AmericaServiceImpl extends BaseService<AmericaResponse> { /** * 實現抽象方法,初始化返回值型別 * @return */ @Override protected AmericaResponse initResponse() { AmericaResponse americaResponse = new AmericaResponse(); americaResponse.setName("美國"); americaResponse.setState(Arrays.asList("紐約","華盛頓")); return americaResponse; } }
6.測試類
package com.example.core.mydemo.java.fanxing; import com.example.core.mydemo.json2.GsonUtils; import com.google.gson.Gson; /** * output: * BaseService method1 * BaseService method2 * {"province":["湖南","山東"],"name":"中國"} * BaseService method1 * BaseService method2 * {"state":["紐約","華盛頓"],"name":"美國"} */ public class FanxingTest { public static void main(String[] args) { BaseService<ChinaResponse> baseService = new ChinaServiceImpl(); ChinaRequest chinaRequest = new ChinaRequest(); Result<ChinaResponse> result = null; try { result = baseService.handle(chinaRequest); } catch (Exception e) { e.printStackTrace(); } ChinaResponse chinaResponse = result.getResponse(); System.out.println(new Gson().toJson(chinaResponse)); BaseService<AmericaResponse> americaService = new AmericaServiceImpl(); AmericaRequest americaRequest = new AmericaRequest(); Result<AmericaResponse> americaResult = null; try { americaResult = americaService.handle(americaRequest); } catch (Exception e) { e.printStackTrace(); } AmericaResponse americaResponse = americaResult.getResponse(); System.out.println(new Gson().toJson(americaResponse)); } }