泛型模板化設計使用-訂單搜尋介面

oktokeep發表於2024-04-21

泛型模板化設計使用-訂單搜尋介面

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));

    }
}

相關文章