工厂&策略模式(大量替换if的使用)
package com.example.demo.config;
import com.example.demo.strategys.TableStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component("handlerContext")
public class HandlerContext {
@Autowired
private ApplicationContext applicationContext;
public static Map<String, Class<TableStrategy>> orderStrategyBeanMap= new HashMap<>();
public TableStrategy getOrderStrategy(String type){
Class<TableStrategy> strategyClass = orderStrategyBeanMap.get(type);
if(strategyClass==null) throw new IllegalArgumentException("没有对应的类型");
return applicationContext.getBean(strategyClass);
}
}
package com.example.demo.config;
import com.example.demo.annotation.HandlerTableType;
import com.example.demo.strategys.TableStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class HandlerProcessor implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, Object> orderStrategyMap = applicationContext.getBeansWithAnnotation(HandlerTableType.class);
orderStrategyMap.forEach((k,v)->{
Class<TableStrategy> orderStrategyClass = (Class<TableStrategy>) v.getClass();
String type = orderStrategyClass.getAnnotation(HandlerTableType.class).value();
HandlerContext.orderStrategyBeanMap.put(type,orderStrategyClass);
});
}
}
package com.example.demo.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface HandlerTableType {
String value();
}
package com.example.demo.strategys;
import org.apache.poi.hssf.usermodel.HSSFSheet;
public interface TableStrategy {
abstract public void handleShow( String[] shows, HSSFSheet sheet);
}
package com.example.demo.strategys.impi;
import com.example.demo.annotation.HandlerTableType;
import com.example.demo.dao.ArticleMapper;
import com.example.demo.entity.Article;
import com.example.demo.strategys.TableStrategy;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@HandlerTableType("article")
public class ArticleTableStrategy implements TableStrategy {
@Autowired
private ArticleMapper mapper;
@Override
public void handleShow( String[] shows, HSSFSheet sheet) {
}
}
}
package com.example.demo.strategys.impi;
import com.example.demo.annotation.HandlerTableType;
import com.example.demo.dao.CronMapper;
import com.example.demo.entity.Cron;
import com.example.demo.strategys.TableStrategy;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@HandlerTableType("socks")
public class SocksTableStrategy implements TableStrategy {
@Autowired
private CronMapper mapper;
@Override
public void handleShow(String[] shows, HSSFSheet sheet) {
}
}
}
HSSFSheet sheet = new HSSFWorkbook().createSheet("socks");
handlerContext.getOrderStrategy("socks").handleShow(new String[]{},sheet);
反射方法(若依框架的定时任务实现思路)
package com.example.demo.config;
import org.springframework.stereotype.Component;
@Component("helloConfig")
public class HelloConfig {
public String hello(String s){
return "hello,"+s;
}
}
Object bean = applicationContext.getBean("helloConfig");
Method method = bean.getClass().getDeclaredMethod("hello", String.class);
Object invoke = method.invoke(bean, new Object[]{"world"});
System.out.println("==="+invoke+"===");