SpringBoot中使用枚举类、switch、常量类(声明并初始化map)实现类策略者模式,接口返回常量数据

场景

SpringBoot中策略模式+工厂模式业务实例(接口传参-枚举类查询策略映射关系-执行不同策略)规避大量if-else:

SpringBoot中策略模式+工厂模式业务实例(接口传参-枚举类查询策略映射关系-执行不同策略)规避大量if-else_霸道流氓气质的博客-CSDN博客

SpringBoot+@Validate+全局异常拦截实现自定义规则参数校验(校验get请求参数不能为空且在指定枚举类型中):

SpringBoot+@Validate+全局异常拦截实现自定义规则参数校验(校验get请求参数不能为空且在指定枚举类型中)_霸道流氓气质的博客-CSDN博客

Java中使用枚举类和switch实现映射存储的类似策略模式实现定制化流程:

Java中使用枚举类和switch实现映射存储的类似策略模式实现定制化流程_霸道流氓气质的博客-CSDN博客

在上面几篇文章的基础上,实现提供一个接口根据请求参数不同返回对应固定的常量值。

比如接口请求需要提供请求码,根据请求码查询对应的策略,并返回常量类中声明和初始化的字符串常量和Map常量。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi 

实现

1、新建枚举类存储请求码与不同对象的策略关系

package com.badao.demo.enums;

import com.badao.demo.constant.Constants;
import org.springframework.lang.Nullable;

import java.util.HashMap;
import java.util.Map;

public enum MineMessageEnum
{
    ZLW("zlw", "0001","badao1",Constants.SIFANGJISIGNAL,null),
    JJT("jjt", "0002","badao2", Constants.SIFANGJISIGNAL,null),
    CCL("ccl", "0005","badao3",Constants.KEERMASIGNAL,Constants.KEERMATURNOUT);

    private final String apiCode;
    private final String mineCode;
    private final String mineName;
    private final String signalRule;
    private final String turnoutRule;

    private static final Map<String, MineMessageEnum> mappings = new HashMap<>();

    static
    {
        for (MineMessageEnum messageEnum : values())
        {
            mappings.put(messageEnum.apiCode, messageEnum);
        }
    }

    @Nullable
    public static MineMessageEnum resolve(@Nullable String mineApiCode)
    {
        return (mineApiCode != null ? mappings.get(mineApiCode) : null);
    }

    MineMessageEnum(String apiCode, String mineCode, String mineName,String signalRule,String turnoutRule)
    {
        this.apiCode = apiCode;
        this.mineCode = mineCode;
        this.mineName = mineName;
        this.signalRule = signalRule;
        this.turnoutRule = turnoutRule;
    }

    public String getApiCode() {
        return apiCode;
    }

    public String getMineCode() {
        return mineCode;
    }

    public String getMineName() {
        return mineName;
    }

    public String getSignalRule() {
        return signalRule;
    }

    public String getTurnoutRule() {
        return turnoutRule;
    }
}

这里存储请求码apiCode与不用业务类型signalRule的对应关系。

这里的signalRule使用常量类存储

package com.badao.demo.constant;

import java.util.HashMap;
import java.util.Map;

public class Constants {

    //枚举类常量
    public static final String SIFANGJISIGNAL = "sifangjiSignal";
    public static final String KEERMASIGNAL = "keermaSignal";

    //字典值常量
    public static final String VEHICLE_TYPE_DICT_TYPE = "vehicle_type";
    public static final String VEHICLE_TYPE_DICT_DESC = "车辆类型";
    public static final String SIGNAL_STATE_DICT_TYPE = "signal_state";
    public static final String SIGNAL_STATE_DICT_DESC = "信号灯状态";
    public static final String SIGNAL_MODEL_DICT_TYPE = "signal_state";
    public static final String SIGNAL_MODEL_DICT_DESC = "信号灯模式";

    public static final Map<String,String> SIFANGJI_SIGNAL_STATE_MAP = new HashMap<String,String>(){{
        put("0","绿灯");
        put("1","红灯");
    }};

    public static final Map<String,String> SIFANGJI_SIGNAL_MODEL_MAP = new HashMap<String,String>(){{
        put("0","就地模式");
        put("1","集控模式");
    }};

    public static final Map<String,String> KEERMA_SIGNAL_STATE_MAP = new HashMap<String,String>(){{
        put("0","掉线");
        put("1","红");
        put("2","绿");
        put("3","黄");
    }};
}

其中声明了字典值的常量类,使用Map进行存储并初始化。

2、接口请求时传递apiCode并校验,具体流程参考上面博客

    @AccessLimit(seconds = 10,maxCount = 2)
    @GetMapping("/dict_values")
    public AjaxResult getDictValues(@Validated MyRequestParams requestParams)
    {
        String mineApiCode = requestParams.getMineApiCode();
        MineMessageEnum resolve = MineMessageEnum.resolve(mineApiCode);
        List<DictDTO> dictDtoList = snDataUploadService.getDictDtoList(resolve);
        return AjaxResult.success(dictDtoList);
    }

然后再service的实现中

    @Override
    public List<DictDTO> getDictDtoList(MineMessageEnum resolve) {
        List<DictDTO> dictDTOList = new ArrayList<>();
        dictDTOList.add(commonServiceUtils.getCarTypeDictMap());
        dictDTOList = commonServiceUtils.getSignalDictMap(resolve,dictDTOList);
        dictDTOList = commonServiceUtils.getTurnoutDictMap(resolve,dictDTOList);
        return  dictDTOList;
    }

封装了不用类型数据的获取

找一个示例

    public List<DictDTO> getSignalDictMap(MineMessageEnum resolve,List<DictDTO> dictDTOList){
        DictDTO signalStateDictDTO = DictDTO.builder().dictType(Constants.SIGNAL_STATE_DICT_TYPE).dictDescription(Constants.SIGNAL_STATE_DICT_DESC).build();
        DictDTO signalModelDictDTO = DictDTO.builder().dictType(Constants.SIGNAL_MODEL_DICT_TYPE).dictDescription(Constants.SIGNAL_MODEL_DICT_DESC).build();
        if(null == resolve.getSignalRule()){
            signalStateDictDTO.setDictMaps(new HashMap<>());
            signalModelDictDTO.setDictMaps(new HashMap<>());
        }else{
            switch (resolve.getSignalRule()){
                case Constants.SIFANGJISIGNAL:
                    signalStateDictDTO.setDictMaps(Constants.SIFANGJI_SIGNAL_STATE_MAP);
                    signalModelDictDTO.setDictMaps(Constants.SIFANGJI_SIGNAL_MODEL_MAP);
                    break;
                case Constants.KEERMASIGNAL:
                    signalStateDictDTO.setDictMaps(Constants.KEERMA_SIGNAL_STATE_MAP);
                    signalModelDictDTO.setDictMaps(Constants.KEERMA_SIGNAL_MODEL_MAP);
                    break;
                default:
                    signalStateDictDTO.setDictMaps(new HashMap<>());
                    signalModelDictDTO.setDictMaps(new HashMap<>());
            }
        }
        dictDTOList.add(signalStateDictDTO);
        dictDTOList.add(signalModelDictDTO);
        return dictDTOList;
    }

3、比如这里接口是返回固定的字典值,所以封装一个实体类,包含字典类型、字典描述、字典值Map

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DictDTO {
    private String dictType;
    private String dictDescription;
    private Map<String,String> dictMaps;
}

运行效果

 

传递不同参数的效果