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

相关推荐
唐青枫5 小时前
Java JDBC 实战指南:从 Connection 到事务和连接池
java
一个做软件开发的牛马6 小时前
MyBatis-Plus 从零实战:完整搭建可运行 Demo,BaseMapper 零 SQL、Wrapper 条件构造、分页插件与代码生成器详解
java·后端
用户3721574261356 小时前
Java 处理 PDF 图片:提取 PDF 中的图片,并压缩 PDF 图片体积
java
用户3721574261356 小时前
Java 打印 Word 文档:从基础打印到高级设置
java
用户3521802454751 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
东坡白菜1 天前
破局全栈:一个前端开发的Java入门实战记录(1)
java·全栈
唐青枫1 天前
Java Tomcat 实战指南:从 Servlet 容器到 Spring Boot 部署
java
wsaaaqqq1 天前
roudan:自由选择实体、灵活操作数据、快速写入数据库的 Java 框架
java
plainGeekDev1 天前
null 判断 → Kotlin 可空类型
android·java·kotlin
糖拌西瓜皮1 天前
Java开发者视角:深入理解Node.js异步编程模型
java·后端·node.js