函数表达式 实现类似策略模式效果

函数表达式 实现类似策略模式效果

1.定义枚举

public enum ReportEnum {


    LABOR_COST("test","test",new FmsTestReportApi())

    /**
     * 报表 code
     */
    private final String code;

    /**
     * 描述
     */
    private final String desc;


    private final ReportApi reportApi;
   }
    
//基础接口
   public interface ReportApi {

    /**
     * 返回报表 所需要表数据状态
     * @param commonReportQueryParam
     * @return
     */
    List<CommonReportResult> report(CommonReportQueryParam commonReportQueryParam);

    /**
     * 获取报表类型枚举
     * @return
     */
    ReportEnum getReportEnum();
   }
  1. 上下文对象 通用调用

      //上下文对象
    public class FmsCommonReportContextApi {
    
        private static Map<String, Function<CommonReportQueryParam,List<CommonReportResult>>> functionMap = new HashMap<>();
    
        static {
            for (ReportEnum reportEnum : ReportEnum.values()) {
                functionMap.put(reportEnum.getCode(), (param)->reportEnum.getReportApi().report(param));
            }
        }
    
        
        public static List<CommonReportResult> getFmsCommonReport(CommonReportQueryParam commonReportQueryParam){
            checkParam(commonReportQueryParam);
            Function<CommonReportQueryParam, List<CommonReportResult>> commonReportQueryParamListFunction = functionMap.get(commonReportQueryParam.getReportEnum().getCode());
            if(null == commonReportQueryParamListFunction){
                throw new RuntimeException("暂未实现XXXXX");
            }
            List<CommonReportResult> commonReportResult = commonReportQueryParamListFunction.apply(commonReportQueryParam);
            return commonReportResult;
        }
    }
    
posted @ 2026-01-30 16:37  川流不息&  阅读(5)  评论(0)    收藏  举报