1、需要一个抽象类
public abstract class AbstractBizHandler { /* 直接后继,用于传递请求 */ protected AbstractBizHandler abstractBizHandler; public void setSuccessor(AbstractBizHandler abstractBizHandler) { this.abstractBizHandler = abstractBizHandler; } /* * 处理具体业务 */ public abstract void doRealBusiness(String biz, Map bizParams); }
2.创建一个工厂,在spring工程中一定要注入建立,不用new,因为后期还有很多方法依赖于springm,new出来的无法注入具体实现类中的bean
Component public class AbstractBizHandlerFactory { /* * 创建工厂方法 */ @Autowired TxxxHandler txxxandler; @Autowired TxxxHandler2 txxxandler2; public AbstractBizHandler createHandler() { txxxandler.setSuccessor(txxxandler2); return txxxandler; } }
3.具体handler
@Component public class TxxxHandler extends AbstractBizHandler { @Override public void doRealBusiness(String str, Map map) { if ("txxx".equals(biz)) { } else { abstractBizHandler.doRealBusiness(str, map); }
4、在实现类中引用
AbstractBizHandler handler = abstractBizHandlerFactory.createHandler(); handler.doRealBusiness(str, map);
这样一个责任链就实现好了
这样有个不好处就是没次新增需要修改工厂,有种方式是用抽象工厂代替现在的工厂,但是这样就太复杂了
下面的方法可以只要新增一个实现类就可以
1、
将抽象类改为这样
@Component public abstract class AbstractBizHandler { public abstract String getBiz(); public abstract void doRealBusiness( Map bizParams); }
实现类改为这样
@Component public class TxxxHandler extends AbstractBizHandler { @Override public String getBiz() { return xxx; } @Override public void doRealBusiness(Map map) { } }
@Component public class AbstractBizHandlerFactory implements InitializingBean { @Autowired private List<AbstractBizHandler> handlers; @Autowired private Map<String, AbstractBizHandler> map; public void getRealHandler2(String str, Map map1) { map.get(biz).doRealBusiness(map1); } @Override public void afterPropertiesSet() throws Exception { handlers.stream().forEach(handler -> { map.put(handler.getBiz(), handler); }); } }
调用方直接调用getRealHandler2就行了
还有一种是这样
*/ @Component public abstract class AbstractBizHandler { public abstract boolean support(String str); public abstract void doRealBusiness( Map map); }
*/ @Component public abstract class AbstractBizHandler { public abstract boolean support(String str); public abstract void doRealBusiness( Map map); }
@Component public class AbstractBizHandlerFactory { @Autowired private List<AbstractBizHandler> handlers; public void getRealHandler(String str, Map map1) { for (AbstractBizHandler handler : handlers) { if (handler.support(str)) { handler.doRealBusiness(map1); break; } } }
factory还有一种办法是这样
private static List<Class<? extends AbstractBizHandler>> abstractBizHandlerList= Lists.newArrayList(); @Autowired private static Map<String, Class<? extends AbstractBizHandler>> abstractBizHandlerMap = Maps.newHashMap() public void getRealHandler(String biz,Map bizParams) throws IllegalAccessException, InstantiationException { AbstractBizHandler abstractBizHandler = abstractBizHandlerMap.get(biz).newInstance(); abstractBizHandler.doRealBusiness(bizParams); } static { //获取该路径下所有类 Reflections reflections = new Reflections("com"); //获取继承了IAnimal的所有类 Set<Class<? extends AbstractBizHandler>> classSet = reflections.getSubTypesOf(AbstractBizHandler.class); abstractBizHandlerList.addAll(classSet); } @PostConstruct public void init() throws IllegalAccessException, InstantiationException { for (Class<? extends AbstractBizHandler> clazz : abstractBizHandlerList){ AbstractBizHandler obj = clazz.newInstance(); abstractBizHandlerMap.put(obj.getBiz(), clazz); } }