当前位置:网站首页>Implementing factory mode using enumeration

Implementing factory mode using enumeration

2022-06-22 16:29:00 Stay for love

For example, there are four dimensions of daily life / Week degree / Customize , Achieve scheduled scheduling , Now each dimension has an implementation class , When creating timers of different dimensions , Will trigger the implementation class to execute

Enumeration class :

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," Day degree ", QuartzDayManager.class),
    WEEK(2," Week degree ", QuartzWeekManager.class),
    MONTH(3," monthly ", QuartzMonthManager.class),
    CUSTOM(4," Customize ", 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 Day degree ,QuartzDayManager Week degree ,QuartzMonthManager monthly
QuartzWeekManager Customize , All inherited CreateTaskQuartzManager class

call :

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

I'm here through CreateTaskQuartzManager createTaskQuartzManager = (CreateTaskQuartzManager) SpringContextHolder.getBean(FrequencyEnum.findClassByCode(ruleSchedulePO.getFrequency())); To make the call ,SpringContextHolder.getBean It's from IOC Get objects from the container , That is to say, one line of code can pass through Frequency Get the corresponding implementation class

SpringContextHolder class :

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;

    /**
     *  Realization ApplicationContextAware Interface context Injection function ,  Put it in a static variable .
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext; // NOSONAR
    }

    /**
     *  Gets the... Stored in a static variable ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }

    /**
     *  From static variables ApplicationContext Get in Bean,  Automatically convert to the type of the assigned object .
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }

    /**
     *  From static variables ApplicationContext Get in Bean,  Automatically convert to the type of the assigned object .
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return (T) applicationContext.getBean(clazz);
    }

    /**
     *  eliminate applicationContext Static variables .
     */
    public static void cleanApplicationContext() {
        applicationContext = null;
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException(
                    "applicaitonContext Not injected , Please be there. applicationContext.xml In the definition of 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();


}

原网站

版权声明
本文为[Stay for love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221507399443.html