使用註解幹掉大量if else和switch

最近正在作工單系統,每種類型工單的操做是類似的可是又有不一樣,好比工單審批是相同的,可是工單啓動是不一樣的,所以天然而然想到把工單的操做放到一個抽象類中,每種工單實現不一樣的操做。前端

工單服務抽象類ProcessInstanceServicebash

@Service
public abstract class ProcessInstanceService{
    public abstract String start(BaseParamDTO baseParamDTO);
    
    public void approval(BaseParamDTO baseParamDTO, String user) {
        System.out.println("審批經過");
    }
}
複製代碼

如今有一種擴容工單和一種權限申請工單app

@Service
public class ExpansionService extends ProcessInstanceService{
    @Override
    public String start(BaseParamDTO baseParamDTO) {
        System.out.println("擴容");
    }
}

@Service
public class PrivilegeService extends ProcessInstanceService{
    @Override
    public String start(BaseParamDTO baseParamDTO) {
        System.out.println("權限申請");
    }
} 
複製代碼

前端啓動工單,這個時候須要根據工單類型獲取對應的服務進行操做,所以使用了工廠模式ide

public enum  WorksheetTypeEnum {

    EXPANSION("擴容", "machine_expansion"),
    PRIVILEGE("登陸權限","privilege_login");
    
    private String cnName;

    private String activitiDefineName;

    WorksheetTypeEnum(String cnName, String activitiDefineName){
        this.cnName = cnName;
        this.activitiDefineName = activitiDefineName;
    }
}

@Component
public class ProcessInstanceFactory {
    @Autowired
    private ExpansionService expansionService;
    
    @Autowired
    private PrivilegeService privilegeService;
    
    public ProcessInstanceService getProcessInstanceService(WorksheetTypeEnum worksheetTypeEnum){
        switch (worksheetTypeEnum){
            case EXPANSION:
                return expansionService;
            case PRIVILEGE:
                return privilegeService;
            default:
                return null;
        }
    }
}
複製代碼

當前的代碼已經使用枚舉簡化了if else,可是仍然存在問題,當新增一種工單時,ProcessInstanceFactory必須注入新的工單服務,而後添加新的case。那有沒有什麼方法不用修改工廠類直接添加新的工單類型呢,那就是使用註解。ui

首先建立一個註解this

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ServiceTypeAnnotation {

    /**
     * 工單類型名稱
     * @return
     */
    WorksheetTypeEnum value();
}
複製代碼

爲每一個工單服務加上註解spa

@Service
@ServiceTypeAnnotation(value = WorksheetTypeEnum.EXPANSION)
public class ExpansionService extends ProcessInstanceService{}

@Service
@ServiceTypeAnnotation(value = WorksheetTypeEnum.PRIVILEGE)
public class PrivilegeService extends ProcessInstanceService{}
複製代碼

修改工廠類ProcessInstanceFactory,在初始化的時候經過上下文拿到全部加了註解的類放到map中code

@Component
public class ProcessInstanceFactory {

    @Autowired
    private DefaultService defaultService;

    @Autowired
    private ApplicationContext applicationContext;

    private Map<WorksheetTypeEnum, ProcessInstanceService> map = Maps.newConcurrentMap();

    @PostConstruct
    public void init(){
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(ServiceTypeAnnotation.class);
        for (Object bean : beansWithAnnotation.values()){
            ServiceTypeAnnotation annotation = bean.getClass().getAnnotation(ServiceTypeAnnotation.class);
            map.put(annotation.value(), (ProcessInstanceService) bean);
        }
    }

    public ProcessInstanceService getProcessInstanceService(WorksheetTypeEnum worksheetTypeEnum){
        ProcessInstanceService processInstanceService = map.get(worksheetTypeEnum);
        if (processInstanceService == null){
            return defaultService;
        } else {
            return processInstanceService;
        }
    }
複製代碼

這樣若是須要新增一個工單服務,只須要對新的類加上註解就能夠了,如新增一個手工工單get

public enum  WorksheetTypeEnum {

    EXPANSION("擴容", "machine_expansion"),
    PRIVILEGE("登陸權限","privilege_login"),
    ASSIGNMENT("手工工單", "assignments");
    
}

@Service
@ServiceTypeAnnotation(value = WorksheetTypeEnum.ASSIGNMENT)
public class AssignmentService extends ProcessInstanceService{}
複製代碼
相關文章
相關標籤/搜索