代码对比

下面是一个请求腾讯云图像识别接口,并对请求结果,请求时间进行记录的方法

1.原始代码

@Override
public OcrIdCardResponse idCardOcr(OcrRequest request) {
        IDCardOCRResponse response = null;
        long startTime = System.currentTimeMillis();
        int status = 0;
        String result = StringUtils.EMPTY;
        try {
            response = tencentOcrClient.idCardOcr(request.getImageUrl());
            recordResult(response);
            result = JsonConvertUtils.objectToJson(response);
        } catch (Exception e) {
            status = 1;
            result = dealException(e);
        } finally {
            insertApiRecord(request, result, status, startTime);
        }

        return FacadeTransformUtil.tencentIdCard2Response(response);
    }

@Override
public OcrVehicleLicenseResponse vehicleLicenseOcr(OcrRequest request) {
        VehicleLicenseOCRResponse response = null;
        long startTime = System.currentTimeMillis();
        int status = 0;
        String result = StringUtils.EMPTY;
        try {
            response = tencentOcrClient.vehicleLicenseOcr(request.getImageUrl());
            recordResult(response);
            result = JsonConvertUtils.objectToJson(response);
        } catch (Exception e) {
            status = 1;
            result = dealException(e);
        } finally {
            insertApiRecord(request, result, status, startTime);
        }
        return FacadeTransformUtil.tencentOcrVehicleLicense2Response(response);
    }

2.优化后代码

@Override
public OcrIdCardResponse idCardOcr(OcrRequest request) {
        IDCardOCRResponse response = request(request, ocrRequest -> tencentOcrClient.idCardOcr(ocrRequest));
        return FacadeTransformUtil.tencentIdCard2Response(response);
    }

    @Override
public OcrVehicleLicenseResponse vehicleLicenseOcr(OcrRequest request) {
        VehicleLicenseOCRResponse response = request(request, ocrRequest -> tencentOcrClient.vehicleLicenseOcr(ocrRequest));
        return FacadeTransformUtil.tencentOcrVehicleLicense2Response(response);
    }

private <R> R request(OcrRequest t, Function<String, R> client) {
        long startTime = System.currentTimeMillis();
        int status = 0;
        String result = StringUtils.EMPTY;
        R r = null;
        try {
            r = client.apply(t.getImageUrl());
        } catch (BizException e) {
            status = 1;
            result = e.getErrorMessage();
        } catch (Exception e) {
            status = 1;
            result = e.getMessage();
        } finally {
            insertApiRecord(t, result, status, startTime);
        }
        if (r == null) {
            throw new BizException(OcrResponseCodeEnum.API_RESULT_EMPTY);
        }
        return r;
    }

解析

R functionA(T r) {
   步骤1
   具体实现(r)
   步骤2
}
S functionB(T r) {
   步骤1
   具体实现(r)
   步骤2
}

像这种只是调用的方法不一样,其他步骤都是一样的情况下,如果不进行封装,就会有大量重复代码,借助lambda表达式和泛型,将重复代码封装一个方法中,将具体实现作为参数传入进来
R 代表返回结果类型
T 代表传入参数

<R> R request(T t, Function<T, R> function)

执行方法时使用

function.apply(t);

调用方使用,传入参数,和具体方法实现

R r = request(t, new Function<T, R>() {
    @Override
    public IDCardOCRResponse apply(String s) {
       return null;
    }
});

可以进一步简写

R r = request(t, t -> 具体实现(t));