在spring项目中使用责任链设计模式

责任链模式的基本类图就不描述了,直接进入正题在spring项目中如何利用spring的机制更优雅的实现责任链设计模式。

第一步:定义handler

/**
 * 条件处理接口
 * @author dfg
 * Created by on 2023-04-22 20:44
 */
public interface ConditionHandler {
    /**
     * 核心的执行方法
     * @param type
     * @return
     */
    Boolean coreExec(String type);

    /**
     * 运行级别
     * @return
     */
    Integer coreRank();
}

接口里面定义了两个函数,第一个函数是拓展的时候要实现的核心方法,第二个函数是决定处理的顺序

/**
 * 抽象类,方便之后拓展
 * @author dfg
 * Created by on 2022-04-22 20:45
 */
public abstract class AbstractConditionHandler implements ConditionHandler {
    
}

再定义一个抽象类,以后拓展更方便,公共方法可以放抽象类

import org.springframework.stereotype.Service;

/**
 * @author dfg
 * Created by on 2023-04-22 20:46
 */
@Service
public class ConditionA extends AbstractConditionHandler {
    @Override
    public Boolean coreExec(String type) {
        System.out.println("this is A");
        if ("A".equals(type)) {
            System.out.println("match A");
            return false;
        }

        return true;
    }

    @Override
    public Integer coreRank() {
        return ConditionRankEnum.CONDITION_A.getRank();
    }
}


import org.springframework.stereotype.Service;

/**
 * @author dfg
 * Created by on 2023-04-22 20:46
 */
@Service
public class ConditionB extends AbstractConditionHandler {
    @Override
    public Boolean coreExec(String type) {
        System.out.println("this is B");
        if ("B".equals(type)) {
            System.out.println("match B");
            return false;
        }

        return true;
    }

    @Override
    public Integer coreRank() {
        return ConditionRankEnum.CONDITION_B.getRank();
    }
}


import org.springframework.stereotype.Service;

/**
 * @author dfg
 * Created by on 2023-04-22 20:46
 */
@Service
public class ConditionC extends AbstractConditionHandler {

    @Override
    public Boolean coreExec(String type) {
        System.out.println("this is C");
        if ("C".equals(type)) {
            System.out.println("match C");
            return false;
        }

        return true;
    }

    @Override
    public Integer coreRank() {
        return ConditionRankEnum.CONDITION_C.getRank();
    }
}


import lombok.Getter;

/**
 * 条件的级别
 * @author dfg
 * Created by on 2023-04-22 20:50
 */
@Getter
public enum ConditionRankEnum {
    DEFAULT_RANK(0, "默认级别"),
    CONDITION_A(1, "A"),
    CONDITION_B(2, "B"),
    CONDITION_C(-1, "C"),
    CONDITION_D(4, "D"),
    ;

    /**
     * 枚举code
     */
    private Integer rank;
    /**
     * 中文描述
     */
    private String msg;

    ConditionRankEnum(Integer rank, String msg) {
        this.rank = rank;
        this.msg = msg;
    }
}

定义了三个实现类和运行级别的枚举,我自己只是为了测试,实际使用的话coreExec的方法入参要定义成对象,这样加参数,其他handler就不会受影响,返回结果也是一样,需要定义成对象。

定义好了这些类,当增加一个handler的时候只需要关心实现哪个类,核心执行逻辑是什么,运行级别设置多大,这三件事。

第二步:定义handler的创建工厂与执行器类

import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 责任链的工厂和执行器
 * @author dfg
 * Created by on 2023-04-22 20:49
 */
@Service
public class ConditionFactoryAndRunner {
    @Autowired
    private List<ConditionHandler> conditionList;

    public ConditionFactoryAndRunner build() {
        conditionList.sort(Comparator.comparing(ConditionHandler::coreRank));
        return this;
    }

    public Boolean exec(String type) {
        Boolean result = false;
        for (ConditionHandler condition : conditionList) {
            if (!(result = condition.coreExec(type))) {
                break;
            }
        }

        return result;
    }
}

创建与执行都放在一起了,逻辑太简单了,简单解释一下

  • build函数:利用spring的@Autowired注解把实现了ConditionHandler的所有实现类都放到list里面,然后build的时候做一个排序,保证执行顺序
  • exec函数:for循环遍历就够了

第三步:使用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 责任链测试
 * @author dfg
 * Created by on 2023-04-22 20:45
 */
@RestController
public class ConditionController {
    @Autowired
    private ConditionFactoryAndRunner conditionFactory;

    @GetMapping("/conditionTest")
    public void conditionTest(String type) {
        conditionFactory.build().exec(type);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值