泛型模板化设计使用-订单搜索接口
泛型模板化设计使用-订单搜索接口
1.定义订单搜索接口
package com.example.core.mydemo.java.templateQuery; //暂不使用该接口 //public interface SearchService<T extends BaseRequest,F extends BaseResponse>{ public interface SearchService<T,F>{ /** * 订单搜索 */ F orderSearch(T reqVO) throws Exception; }
2.定义抽象类
package com.example.core.mydemo.java.templateQuery; import java.io.IOException; /** * 接口和抽象实现类不能同时含有<T,F> * 'orderSearch(T)' in 'com.example.core.mydemo.java.templateQuery.AbstractSearchTemplate' clashes with 'orderSearch(T)' in 'com.example.core.mydemo.java.templateQuery.SearchService'; both methods have same erasure, yet neither overrides the other * @param <T> * @param <F> */ //public abstract class AbstractSearchTemplate<T,F> implements SearchService{ //public abstract class AbstractSearchTemplate<T extends BaseRequest,F extends BaseResponse>{ //去掉 实现 SearchService public abstract class AbstractSearchTemplate<T,F>{ //这样定义也可以,不过 T extends BaseRequest,F extends BaseResponse 规范了类型 public F orderSearch(T reqVO) throws Exception { return templateMethod(reqVO); } private F templateMethod(T reqVO) throws Exception { System.out.println("构建查询条件,准备参数"); page(reqVO); System.out.println("构建查询条件,分页"); filter(reqVO); System.out.println("构建查询条件,过滤"); sort(reqVO); System.out.println("构建查询条件,排序"); return transform(reqVO); } /** * 分页 */ protected abstract void page(T reqVO); /** * 过滤 */ protected abstract void filter(T reqVO); /** * 排序 */ protected abstract void sort(T reqVO); /** * 转换 */ protected abstract F transform(T reqVO) throws IOException; }
3.定义请求基类和返回基类
package com.example.core.mydemo.java.templateQuery; public class BaseRequest { } package com.example.core.mydemo.java.templateQuery; public class BaseResponse { }
4.第一个实现类
package com.example.core.mydemo.java.templateQuery; public class AppRequest extends BaseRequest{ /** * app订单号 */ private String orderNo; public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } } package com.example.core.mydemo.java.templateQuery; public class AppResponse extends BaseResponse{ /** * 返回手机号 */ private String mobile; /** * 返回姓名 */ private String realName; public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } } package com.example.core.mydemo.java.templateQuery; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import java.io.IOException; public class AppSearchServiceImpl extends AbstractSearchTemplate<AppRequest,AppResponse>{ @Override protected void page(AppRequest reqVO) { System.out.println("分页:" + new Gson().toJson(reqVO)); } @Override protected void filter(AppRequest reqVO) { System.out.println("过滤:" + new Gson().toJson(reqVO)); } @Override protected void sort(AppRequest reqVO) { System.out.println("排序:" + new Gson().toJson(reqVO)); } @Override protected AppResponse transform(AppRequest reqVO) throws IOException { System.out.println("数据转换:" + new Gson().toJson(reqVO)); AppResponse appResponse = new AppResponse(); appResponse.setMobile("13100000000"); appResponse.setRealName("测试"); //外部调用返回的json字符串,模拟将json转换到response对象 AppResponse t = JSONObject.parseObject(new Gson().toJson(appResponse), AppResponse.class); return t; } }
5.第二个实现类
package com.example.core.mydemo.java.templateQuery; public class ConsoleRequest extends BaseRequest{ /** * 管理后台交易来源查询 */ private String source; public String getSource() { return source; } public void setSource(String source) { this.source = source; } } package com.example.core.mydemo.java.templateQuery; public class ConsoleResponse extends BaseResponse{ /** * 管理后台操作人 */ private String operator; /** * 订单时间 */ private String orderTime; public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public String getOrderTime() { return orderTime; } public void setOrderTime(String orderTime) { this.orderTime = orderTime; } } package com.example.core.mydemo.java.templateQuery; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import java.io.IOException; public class ConsoleSearchServiceImpl extends AbstractSearchTemplate<ConsoleRequest,ConsoleResponse>{ @Override protected void page(ConsoleRequest reqVO) { System.out.println("分页:" + new Gson().toJson(reqVO)); } @Override protected void filter(ConsoleRequest reqVO) { System.out.println("过滤:" + new Gson().toJson(reqVO)); } @Override protected void sort(ConsoleRequest reqVO) { System.out.println("排序:" + new Gson().toJson(reqVO)); } @Override protected ConsoleResponse transform(ConsoleRequest reqVO) throws IOException { System.out.println("数据转换:" + new Gson().toJson(reqVO)); ConsoleResponse consoleResponse = new ConsoleResponse(); consoleResponse.setOperator("超级管理员"); consoleResponse.setOrderTime("2024-04-19 16:41:00"); //外部调用返回的json字符串,模拟将json转换到response对象 ConsoleResponse t = JSONObject.parseObject(new Gson().toJson(consoleResponse), ConsoleResponse.class); return t; } }
6.测试类
package com.example.core.mydemo.java.templateQuery; import com.google.gson.Gson; /** * output: * 构建查询条件,准备参数 * 分页:{"orderNo":"DD123456789"} * 构建查询条件,分页 * 过滤:{"orderNo":"DD123456789"} * 构建查询条件,过滤 * 排序:{"orderNo":"DD123456789"} * 构建查询条件,排序 * 数据转换:{"orderNo":"DD123456789"} * app查询返回结果:{"mobile":"13100000000","realName":"测试"} * 构建查询条件,准备参数 * 分页:{"source":"3"} * 构建查询条件,分页 * 过滤:{"source":"3"} * 构建查询条件,过滤 * 排序:{"source":"3"} * 构建查询条件,排序 * 数据转换:{"source":"3"} * console查询返回结果:{"operator":"超级管理员","orderTime":"2024-04-19 16:41:00"} */ public class TemplateQueryTest { public static void main(String[] args) throws Exception{ AppSearchServiceImpl app = new AppSearchServiceImpl(); AppRequest appRequest = new AppRequest(); appRequest.setOrderNo("DD123456789"); AppResponse appResponse = app.orderSearch(appRequest); System.out.println("app查询返回结果:" + new Gson().toJson(appResponse)); ConsoleSearchServiceImpl console = new ConsoleSearchServiceImpl(); ConsoleRequest consoleRequest = new ConsoleRequest(); consoleRequest.setSource("3"); ConsoleResponse consoleResponse = console.orderSearch(consoleRequest); System.out.println("console查询返回结果:" + new Gson().toJson(consoleResponse)); } }