需求背景说明:
系统根据不同报警类型进行不同的通知操作,原写法如下:
由于上述case的内容为魔法值,根据要求需要使用枚举类型进行存储。
方式一:case后的内容不动,仅修改魔法值为枚举类型
-
新建枚举类,添加赋值方法
/**
- 报警推送类型
*/
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); }
}
- 报警推送类型
-
修改方法
使用枚举类的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后的内容也合并至枚举类中
-
枚举类:
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); }
}
-
调用方法
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;
}