责任链设计模式详解

简介: 该内容主要介绍了如何使用Java实现责任链模式。

 1、需要一个抽象类

public abstract class AbstractBizHandler {
/*
      直接后继,用于传递请求
*/
    protected AbstractBizHandler abstractBizHandler;
    public void setSuccessor(AbstractBizHandler abstractBizHandler) {
        this.abstractBizHandler = abstractBizHandler;
    }
/*
     * 处理具体业务
     */
    public abstract void doRealBusiness(String biz, Map bizParams);
}

image.gif

2.创建一个工厂,在spring工程中一定要注入建立,不用new,因为后期还有很多方法依赖于springm,new出来的无法注入具体实现类中的bean

Component
public class AbstractBizHandlerFactory {
    /*
     * 创建工厂方法
     */
    @Autowired
    TxxxHandler txxxandler;
    @Autowired
    TxxxHandler2 txxxandler2;
    public  AbstractBizHandler createHandler() {
        txxxandler.setSuccessor(txxxandler2);
        return txxxandler;
    }
}

image.gif

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

image.gif

4、在实现类中引用

AbstractBizHandler handler = abstractBizHandlerFactory.createHandler();
            handler.doRealBusiness(str, map);

image.gif

这样一个责任链就实现好了

这样有个不好处就是没次新增需要修改工厂,有种方式是用抽象工厂代替现在的工厂,但是这样就太复杂了

下面的方法可以只要新增一个实现类就可以

1、

将抽象类改为这样

@Component
public abstract class AbstractBizHandler {
  
    public abstract String getBiz();
   
    public abstract void doRealBusiness( Map bizParams);
}

image.gif

实现类改为这样

@Component
public class TxxxHandler extends AbstractBizHandler {
    @Override
    public String getBiz() {
        return xxx;
    }
    @Override
    public void doRealBusiness(Map map) {
       
    }
}

image.gif

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

image.gif

调用方直接调用getRealHandler2就行了

还有一种是这样

*/
@Component
public abstract class AbstractBizHandler {
    public abstract boolean support(String str);
    
    public abstract void doRealBusiness( Map map);
}

image.gif

*/
@Component
public abstract class AbstractBizHandler { 
    public abstract boolean support(String str);
    
    public abstract void doRealBusiness( Map map);
}

image.gif

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

image.gif

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

image.gif

感谢:https://my.oschina.net/pengyan5945/blog/593634

Java获取所有子类、实现类-CSDN博客

如何获取某个类的所有子类_获取抽象类所有子类-CSDN博客

Java模式之责任链模式_责任链 java-CSDN博客

责任链模式(Chain of Responsibility) Java实现-CSDN博客

目录
相关文章
|
4月前
|
设计模式 Java
常用设计模式(工厂方法,抽象工厂,责任链,装饰器模式)
有关设计模式的其他常用模式请参考 单例模式的实现 常见的设计模式(模板与方法,观察者模式,策略模式)
40 2
|
4月前
|
设计模式 算法 调度
行为型设计模式:模板设计模式/观察者设计模式/策略设计模式/责任链设计模式
行为型设计模式:模板设计模式/观察者设计模式/策略设计模式/责任链设计模式
33 0
|
5月前
|
设计模式
二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
|
7月前
|
设计模式 Java 数据库连接
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
|
12月前
|
设计模式 分布式计算 大数据
大数据开发基础的设计模式的责任链
当涉及大数据的开发时,设计模式是至关重要的。其中一种常见的设计模式是责任链模式,它可以有效地处理多个对象之间的请求。
65 0
|
设计模式 前端开发 数据安全/隐私保护
前端通用编程基础的设计模式之责任链
在前端开发中,我们常常需要处理一些复杂的业务逻辑,例如表单验证、权限控制等。这些业务逻辑可能需要经过多个步骤才能完成,每个步骤都需要进行具体的处理和判断。这时候就需要使用责任链模式来实现业务逻辑的流程化和扩展性。
103 0
|
设计模式 前端开发 JavaScript
前端通用编程基础的设计模式之责任链
在前端开发中,我们经常需要处理复杂的业务逻辑和数据流转。为了解决这些问题,设计模式中的责任链模式可以帮助我们快速地构建一个多级处理机制,从而使得代码更加简洁、可维护并且易于扩展。
80 0
|
设计模式
24种设计模式-责任链设计模式
24种设计模式-责任链设计模式
|
设计模式 前端开发 Java
|
设计模式 XML 存储
设计模式之责任链
设计模式之责任链
161 1
设计模式之责任链