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

Scenes

Design pattern - example of strategy pattern usage in Java:

Design Patterns - Examples of Strategy Patterns Used in Java

The above is an example of the use of the strategy pattern in Java.

However, in some scenarios, strict rule distinction is not required, and different code logics only need to be implemented according to the passed parameters and the processing logic mapping of custom storage.

With the help of the enumeration class, the mapping relationship can be stored in the static code block, and the corresponding processing logic can be obtained according to the passed parameters in the actual business layer.

It is judged by switch.

Note:

Blog:
Overbearing rogue temperament blog_CSDN Blog-C#, Architecture Road, Blogger in SpringBoot

accomplish

1. First define the enumeration class

public enum EcuPrefixEnum
{
    CAR_NUM,  ECU_CAR_NUM;

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

    static
    {
        mappings.put(MineMessageEnum.JJT.getApiCode(),CAR_NUM);
        mappings.put(MineMessageEnum.ZLW.getApiCode(),ECU_CAR_NUM);
    }

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

}

Here two enum variables are defined in different ways.

Among them, the mapping relationship is initialized and stored in the static, and the key in the map is another enumeration class. This enumeration class is used to store information.

It can also be hard-coded directly using a string.

mappings.put("jjt",CAR_NUM);

Here MineMessageEnum is used to store mapping relationship information.

For example, if you need to obtain different mineCode and mineName through apiCode, you can pass

MineMessageEnum.resolve(mineApiCode).getMineCode()
MineMessageEnum.resolve(mineApiCode).getMineName()

to fulfill.

MineMessageEnum code:

public enum MineMessageEnum
{
    JJT("jjt", "0001","名称1", Constants.SIFANGJI),
    ZLW("zlw", "0002","名称2",Constants.SIFANGJI),
    CCL("ccl", "0003","名称3",Constants.KEERMA);

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

    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)
    {
        this.apiCode = apiCode;
        this.mineCode = mineCode;
        this.mineName = mineName;
        this.signalRule = signalRule;
    }

    public String getApiCode() {
        return apiCode;
    }

    public String getMineCode() {
        return mineCode;
    }

    public String getMineName() {
        return mineName;
    }

    public String getSignalRule() {
        return signalRule;
    }
}

Constants above is a constant class

public class Constants {

    public static final String SIFANGJI = "sifangji";
    public static final String KEERMA = "keerma";

}

The following resolve is used to obtain the corresponding value in the map according to the passed parameters.

2. In actual business, different logical processing can be performed according to the passed parameters and switch

    public  BusCarEcu getBusCarEcu(String carNumber,String mineCode) {
        try {
            String ecuKeyPrefix;
            switch (EcuPrefixEnum.resolve(mineCode)){
                case CAR_NUM:
                    ecuKeyPrefix = carNumber;
                    break;
                case ECU_CAR_NUM:
                    ecuKeyPrefix = Constants.ECU+carNumber;
                    break;
                default:
                    ecuKeyPrefix = Constants.ECU+carNumber;
            }
            BusCarEcu busCarEcu = redisCache.getCacheObject(ecuKeyPrefix);
            return busCarEcu;
        }catch (Exception exception){
            System.out.println("getBusCarEcu结果异常:"+exception.getMessage());
            return new BusCarEcu();
        }
    }

The above result is to obtain the corresponding mineCode and mineName when passing mineCode as jjt, and when executing the getBusCarEcu business method,

Execute different business logic according to different mineCode.

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/130502199