java使用枚举类的方式存储魔法值

需求背景说明:

系统根据不同报警类型进行不同的通知操作,原写法如下:

由于上述case的内容为魔法值,根据要求需要使用枚举类型进行存储。

方式一:case后的内容不动,仅修改魔法值为枚举类型

  1. 新建枚举类,添加赋值方法

    /**

    • 报警推送类型
      */

    public enum AlarmNoticeType {

    复制代码
     //邮件
     E_MAIL("E-mail"),
     //    页面
     PAGE("page"),
     //    飞书
     FEISHU("feishu"),
     //    企业微信
     WECOM("weCom"),
     //    app
     APP("app");
    
     private final String type;
    
     AlarmNoticeType(String type) {
         this.type = type;
     }
    
     public static AlarmNoticeType fromValue(String value) {
         for (AlarmNoticeType type : values()) {
             if (type.type.equalsIgnoreCase(value)) {
                 return type;
             }
         }
         throw new IllegalArgumentException("Invalid notice type: " + value);
     }

    }

  2. 修改方法
    使用枚举类的fromValue方法进行赋值,然后使用枚举类的内容进行操作。

    AlarmNoticeType type = AlarmNoticeType.fromValue(noticeTypes[i]);
    switch (type) {
    case E_MAIL:
    try {
    Map<String, Boolean> map = new HashMap<>();
    map.put("E-mail", mailService.sendMailForLoginPwd(mail));
    results.add(map);
    } catch (Exception e) {
    e.printStackTrace();
    }
    break;
    case PAGE:
    break;
    case FEISHU:
    XxlJobHelper.log("飞书发送消息!");
    feiShuUtil.sendFeishuMsg(weCom, notice.getRoleIds());
    break;
    case WECOM:
    weComUtil.sendWeComAlarmMsg(weCom, notice.getRoleIds());
    break;
    case APP:
    break;
    }

方法二:case后的内容也合并至枚举类中

  1. 枚举类:

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

    // 假设存在这些相关的工具类和实体类已经正确导入
    import com.example.service.MailService;
    import com.example.util.FeiShuUtil;
    import com.example.util.WeComUtil;

    public enum NoticeType {
    E_MAIL("E-mail") {
    @Override
    public void execute(MailService mailService, Object mail, List<Map<String, Boolean>> results) {
    Map<String, Boolean> map = new HashMap<>();
    try {
    map.put("E-mail", mailService.sendMailForLoginPwd(mail));
    results.add(map);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    },
    PAGE("page") {
    @Override
    public void execute(MailService mailService, Object mail, List<Map<String, Boolean>> results) {
    // 这里暂时没有具体逻辑,可后续按需添加
    }
    },
    FEISHU("feishu") {
    @Override
    public void execute(MailService mailService, Object mail, List<Map<String, Boolean>> results, Object weCom, List roleIds) {
    XxlJobHelper.log("飞书发送消息!");
    feiShuUtil.sendFeishuMsg(weCom, roleIds);
    }
    },
    WECOM("weCom") {
    @Override
    public void execute(MailService mailService, Object mail, List<Map<String, Boolean>> results, Object weCom, List roleIds) {
    weComUtil.sendWeComAlarmMsg(weCom, roleIds);
    }
    },
    APP("app") {
    @Override
    public void execute(MailService mailService, Object mail, List<Map<String, Boolean>> results) {
    // 这里暂时没有具体逻辑,可后续按需添加
    }
    };

    复制代码
     private final String type;
    
     NoticeType(String type) {
         this.type = type;
     }
    
     public abstract void execute(MailService mailService, Object mail, List<Map<String, Boolean>> results, Object weCom, List<String> roleIds);
    
     public static NoticeType fromValue(String value) {
         for (NoticeType type : values()) {
             if (type.type.equalsIgnoreCase(value)) {
                 return type;
             }
         }
         throw new IllegalArgumentException("Invalid notice type: " + value);
     }

    }

  2. 调用方法

    NoticeType type = NoticeType.fromValue(noticeTypes[i]);
    switch (type) {
    case E_MAIL:
    type.execute(mailService, mail, results);
    break;
    case PAGE:
    type.execute(mailService, mail, results, null, null);
    break;
    case FEISHU:
    type.execute(mailService, mail, results, weCom, roleIds);
    break;
    case WECOM:
    type.execute(mailService, mail, results, weCom, roleIds);
    break;
    case APP:
    type.execute(mailService, mail, results);
    break;
    }

相关推荐
神仙别闹3 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
Wang's Blog3 小时前
Java 接入Redis: 服务启动停止与后台运行配置
java·服务器·redis
此时不提桶,更待何时4 小时前
01-04-B-垃圾回收面试与生产事故实战
java·jvm·面试
伞伞悦读4 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
鹿角片ljp5 小时前
KV Cache 解析
java·算法
captain3766 小时前
网络原理(9)-数据链路层
java·网络协议·java-ee
惊讶的猫6 小时前
意图识别置信度和检索重排的分数
java
2601_966949657 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
caoerzhong7 小时前
仓库管理数字化趋势解读:JeeWMS 开源 Java WMS 视角下的五个演进方向
java·开源