使用枚举实现工厂模式

如现在有从四个维度日度/周度/自定义,实现定时调度,现在每个维度有一个实现类,当创建不同维度的定时器的时候,就会触发实现类去执行

枚举类:

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();


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值