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

相关推荐
Ulyanov几秒前
基于Impress.js的3D概念地图设计与实现
开发语言·前端·javascript·3d·ecmascript
SunnyDays1011几秒前
如何使用 Java 自动调整 Excel 行高和列宽
java·自动调整行高和列宽·自适应行高和列宽
A南方故人5 分钟前
一个用于实时检测 web 应用更新的 JavaScript 库
开发语言·前端·javascript
虎头金猫5 分钟前
内网导航站 “出圈”!用 cpolar 解锁 Dashy 远程访问新玩法
java·c++·python·程序人生·职场和发展·php·程序员创富
JosieBook6 分钟前
【WinForm】使用C# WinForm实现带有托盘图标功能的应用程序
开发语言·c#
2301_790300968 分钟前
C++与量子计算模拟
开发语言·c++·算法
康小庄9 分钟前
SpringBoot 拦截器 (Interceptor) 与切面 (AOP):示例、作用、及适用场景
java·数据库·spring boot·后端·mysql·spring·spring cloud
不会c+12 分钟前
Maven私服的搭建与使用
java·maven
青灯照颦微15 分钟前
【R】三种方式安装R包
开发语言·r语言
weixin_4365250721 分钟前
若依多租户版: RuoYi-Vue-Plus
java