责任链模式的基本类图就不描述了,直接进入正题在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);
}
}