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

相关推荐
野生技术架构师12 分钟前
牛客网Java 高频面试题总结(2025最新版)
java·开发语言·面试
一只鹿鹿鹿12 分钟前
系统安全设计方案书(Word)
开发语言·人工智能·web安全·需求分析·软件系统
纪莫21 分钟前
技术面:SpringBoot(springboot的类加载和传统的双亲委派有什么区别、如何按顺序实例化Bean)
java·spring·java面试⑧股
持梦远方22 分钟前
【C++日志库】启程者团队开源:轻量级高性能VoyLog日志库完全指南
开发语言·c++·visual studio
聪明努力的积极向上23 分钟前
【C#】HTTP中URL编码方式解析
开发语言·http·c#
kyle~39 分钟前
CPU调度---协程
java·linux·服务器·数据库·c++20
会飞的小蛮猪43 分钟前
Skywalking运维之路(Skywalking服务搭建)
java·运维·监控
嵌入式-老费43 分钟前
自己动手写深度学习框架(快速学习python和关联库)
开发语言·python·学习
ctgu901 小时前
PyQt5(八):ui设置为可以手动随意拉伸功能
开发语言·qt·ui
L.EscaRC1 小时前
Redisson在Spring Boot中的高并发应用解析
java·spring boot·后端