In SpringBoot, enumeration classes, switch, and constant classes (declaring and initializing map) are used to implement the class strategist mode, and the interface returns constant data

Scene

Strategy mode + factory mode business instance in SpringBoot (interface parameter transfer-enumeration class query strategy mapping relationship-execution of different strategies) avoids a large number of if-else:

Strategy mode + factory mode business instance in SpringBoot (interface parameter transfer-enumeration class query strategy mapping relationship-execution of different strategies) to avoid a large number of if-else_Overbearing rogue temperament blog-CSDN blog

SpringBoot + @Validate + global exception interception implements custom rule parameter verification (verification get request parameters cannot be empty and in the specified enumeration type):

SpringBoot + @Validate + global exception interception to implement custom rule parameter verification (verification get request parameters cannot be empty and in the specified enumeration type)_Overbearing rogue temperament blog-CSDN blog

The similar strategy pattern of using enumeration class and switch to realize map storage in Java realizes the customization process:

Using enumeration classes and switches in Java to implement similar strategy patterns for mapping storage to achieve customized processes – Programmer Sought

On the basis of the above articles, the implementation provides an interface to return corresponding fixed constant values according to different request parameters.

For example, an interface request needs to provide a request code, query the corresponding strategy according to the request code, and return the string constants and Map constants declared and initialized in the constant class.

Note:

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

Implementation

1. Create a new enumeration class to store the strategic relationship between the request code and different objects

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

The corresponding relationship between the request code apiCode and the signalRule of different business types is stored here.

The signalRule here uses constant class storage

package com.badao.demo.constant;

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

public class Constants {

    // enum class constant
    public static final String SIFANGJISIGNAL = "sifangjiSignal";
    public static final String KEERMASIGNAL = "keermaSignal";

    // dictionary value constant
    public static final String VEHICLE_TYPE_DICT_TYPE = "vehicle_type";
    public static final String VEHICLE_TYPE_DICT_DESC = "Vehicle Type";
    public static final String SIGNAL_STATE_DICT_TYPE = "signal_state";
    public static final String SIGNAL_STATE_DICT_DESC = "Signal Light State";
    public static final String SIGNAL_MODEL_DICT_TYPE = "signal_state";
    public static final String SIGNAL_MODEL_DICT_DESC = "Signal light mode";

    public static final Map<String,String> SIFANGJI_SIGNAL_STATE_MAP = new HashMap<String,String>(){<!-- -->{
        put("0","green light");
        put("1","red light");
    }};

    public static final Map<String,String> SIFANGJI_SIGNAL_MODEL_MAP = new HashMap<String,String>(){<!-- -->{
        put("0","in-place mode");
        put("1","centralized control mode");
    }};

    public static final Map<String,String> KEERMA_SIGNAL_STATE_MAP = new HashMap<String,String>(){<!-- -->{
        put("0","dropped");
        put("1","red");
        put("2","green");
        put("3","yellow");
    }};
}

The constant class of the dictionary value is declared, stored and initialized using Map.

2. Pass the apiCode and verify it when the interface is requested. For the specific process, refer to the above blog

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

Then in the implementation of 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;
    }

Encapsulates the acquisition of different types of data

find an example

 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. For example, the interface here returns a fixed dictionary value, so an entity class is encapsulated, including dictionary type, dictionary description, and dictionary value Map

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

running result

The effect of passing different parameters