当前位置:网站首页>使用枚举实现工厂模式
使用枚举实现工厂模式
2022-06-22 15:10:00 【为爱停留】
如现在有从四个维度日度/周度/自定义,实现定时调度,现在每个维度有一个实现类,当创建不同维度的定时器的时候,就会触发实现类去执行
枚举类:
package com.htf.damp.custom_enum;
import com.htf.damp.quartz.QuartzCustomManager;
import com.htf.damp.quartz.QuartzDayManager;
import com.htf.damp.quartz.QuartzMonthManager;
import com.htf.damp.quartz.QuartzWeekManager;
public enum FrequencyEnum {
DAY(1,"日度", QuartzDayManager.class),
WEEK(2,"周度", QuartzWeekManager.class),
MONTH(3,"月度", QuartzMonthManager.class),
CUSTOM(4,"自定义", QuartzCustomManager.class);
private Integer code;
private String value;
private Class aClass;
private FrequencyEnum(Integer code,String value,Class aClass){
this.code=code;
this.value=value;
this.aClass=aClass;
}
public Integer getCode() {
return code;
}
public String getValue() {
return value;
}
public static String findValueByCode(Integer code){
for(FrequencyEnum v : values()){
if(v.code.equals(code)){
return v.value;
}
}
return null;
}
public static Class findClassByCode(Integer code){
for(FrequencyEnum v : values()){
if(v.code.equals(code)){
return v.aClass;
}
}
return null;
}
}
QuartzCustomManager 日度,QuartzDayManager 周度,QuartzMonthManager月度
QuartzWeekManager自定义,都继承了CreateTaskQuartzManager类
调用:
void processSchedule(RuleSchedulePO ruleSchedulePO,RuleExecutorPO ruleExecutorPO, String tableName,String executeType) {
String genMode = ruleSchedulePO.getTimeGenMode();
CreateTaskModel createTaskModel = new CreateTaskModel();
createTaskModel.setExecutorId(ruleExecutorPO.getExecutorId());
createTaskModel.setGenCode(genMode);
createTaskModel.setRuleId(ruleExecutorPO.getRuleId());
createTaskModel.setScheduleId(ruleSchedulePO.getId());
createTaskModel.setIsSkipWeekend(ruleSchedulePO.getIsSkipWeekend());
createTaskModel.setOverTime(ruleSchedulePO.getOverTime());
createTaskModel.setStartTime(ruleSchedulePO.getStartTime());
createTaskModel.setTableName(tableName);
createTaskModel.setFrequency(ruleSchedulePO.getFrequency());
createTaskModel.setScheduleTime(ruleSchedulePO.getScheduleTime());
CreateTaskQuartzManager createTaskQuartzManager = (CreateTaskQuartzManager) SpringContextHolder.getBean(FrequencyEnum.findClassByCode(ruleSchedulePO.getFrequency()));
if("addJob".equals(executeType)){
createTaskQuartzManager.addJob( createTaskModel);
}else{
createTaskQuartzManager.deleteJob(createTaskModel);
}
}我这里通过CreateTaskQuartzManager createTaskQuartzManager = (CreateTaskQuartzManager) SpringContextHolder.getBean(FrequencyEnum.findClassByCode(ruleSchedulePO.getFrequency()));来进行调用,SpringContextHolder.getBean是从IOC容器里面获取对象,就是说一行代码就可以通过Frequency取出对应的实现类
SpringContextHolder类:
package com.htf.damp.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBean(clazz);
}
/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException(
"applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
public static void setHttpRequestResponseHolder(HttpServletRequest request, HttpServletResponse response){
responseThreadLocal.set(response);
ApplicationContext ap = WebApplicationContextUtils.getWebApplicationContext(null);
}
public static HttpServletResponse getHttpResponse(){
return responseThreadLocal.get();
}
public static void clean(){
responseThreadLocal.remove();
}
private static final ThreadLocal<HttpServletResponse> responseThreadLocal = new ThreadLocal();
}
边栏推荐
- SAP教程中的ALV报告 - ABAP列表查看器-012
- Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
- Huawei cloud hcdez special session and Distributed Technology Summit: Huawei cloud distributed cloud native technology and Practice
- find命令使用
- 知识管理在业务中的价值如何体现
- vector的模拟实现
- 北京恢复堂食半月记:如何重燃门店经营烟火气
- Rosbag use command
- 畅享高性能计算!天翼云HPC解决方案来了
- Google Chrome small details
猜你喜欢
随机推荐
odoo部署到服务器并配置为服务
SAP abap 数据类型,操作符和编辑器-02
SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
jmeter关联登录302类型的接口
5. reading and writing of documents (students)
浙江创投圈的“半壁江山”,还得是国资
【山大会议】项目引入 Redux
Rosbag use command
[Shanda conference] project introduces Redux
在JFlash中添加未知类型的单片机
What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
校企联合在路上!华为云GaussDB又来高校啦
The difference between nvarchar and varchar
Mr. Du built a domestic non garlic Statistics Platform
Cve-2022-0847 (privilege lifting kernel vulnerability)
SAP ABAP 表控制与示例-07
Dear students, don't read the textbooks any more. Just read this one for the complexity of time
SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
Mysql触发器
4. string (reverse order and case conversion)




![[Shangshui Shuo series] day three - VIDEO](/img/42/0911fee2a36f6dda345a571a31acd5.png)




