责任链模式怎么使用呢?
一个接口或者抽象类
每个对象差异化处理
对象链(数组)初始化(连起来)
2.3.1 一个接口或者抽象类
这个接口或者抽象类,需要:
有一个指向责任下一个对象的属性
一个设置下一个对象的set方法
给子类对象差异化实现的方法(如以下代码的doFilter方法)
/**
* 关注公众号:捡田螺的小男孩
*/
public abstract class AbstractHandler {
//责任链中的下一个对象
private AbstractHandler nextHandler;
/**
* 责任链的下一个对象
*/
public void setNextHandler(AbstractHandler nextHandler){
this.nextHandler = nextHandler;
}
/**
* 具体参数拦截逻辑,给子类去实现
*/
public void filter(Request request, Response response) {
doFilter(request, response);
if (getNextHandler() != null) {
getNextHandler().filter(request, response);
}
}
public AbstractHandler getNextHandler() {
return nextHandler;
}
abstract void doFilter(Request filterRequest, Response response);
}
2.3.2 每个对象差异化处理
责任链上,每个对象的差异化处理,如本小节的业务场景,就有参数校验对象、安全校验对象、黑名单校验对象、规则拦截对象
/**
* 参数校验对象
**/
@Component
@Order(1) //顺序排第1,最先校验
public class CheckParamFilterObject extends AbstractHandler {
@Override
public void doFilter(Request request, Response response) {
System.out.println("非空参数检查");
}
}
/**
* 安全校验对象
*/
@Component
@Order(2) //校验顺序排第2
public class CheckSecurityFilterObject extends AbstractHandler {
@Override
public void doFilter(Request request, Response response) {
//invoke Security check
System.out.println("安全调用校验");
}
}
/**
* 黑名单校验对象
*/
@Component
@Order(3) //校验顺序排第3
public class CheckBlackFilterObject extends AbstractHandler {
@Override
public void doFilter(Request request, Response response) {
//invoke black list check
System.out.println("校验黑名单");
}
}
/**
* 规则拦截对象
*/
@Component
@Order(4) //校验顺序排第4
public class CheckRuleFilterObject extends AbstractHandler {
@Override
public void doFilter(Request request, Response response) {
//check rule
System.out.println("check rule");
}
}
2.3.3 对象链连起来(初始化)&& 使用
@Component("ChainPatternDemo")
public class ChainPatternDemo {
//自动注入各个责任链的对象
@Autowired
private List abstractHandleList;
private AbstractHandler abstractHandler;
//spring注入后自动执行,责任链的对象连接起来
@PostConstruct
public void initializeChainFilter(){
for(int i = 0;i
运行结果如下:
非空参数检查
安全调用校验
校验黑名单
check rule
完整代码如下
package com.wenxiaowu.dp.chain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.PostConstruct;
import java.util.List;
@SpringBootApplication(scanBasePackages = {"com.wenxiaowu.dp.chain"})
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ChainPatternTest {
@Autowired
private ChainPatternDemo chainPattern;
@Test
public void test() {
chainPattern.exec(new Request(), new Response());
}
}
@Component
class ChainPatternDemo {
//自动注入各个责任链的对象
@Autowired
private List abstractHandleList;
private AbstractHandler abstractHandler;
//spring注入后自动执行,责任链的对象连接起来
@PostConstruct
public void initializeChainFilter() {
for (int i = 0; i < abstractHandleList.size(); i++) {
if (i == 0) {
abstractHandler = abstractHandleList.get(0);
} else {
AbstractHandler currentHander = abstractHandleList.get(i - 1);
AbstractHandler nextHander = abstractHandleList.get(i);
currentHander.setNextHandler(nextHander);
}
}
}
//直接调用这个方法使用
public Response exec(Request request, Response response) {
abstractHandler.filter(request, response);
return response;
}
public AbstractHandler getAbstractHandler() {
return abstractHandler;
}
public void setAbstractHandler(AbstractHandler abstractHandler) {
this.abstractHandler = abstractHandler;
}
}
class Request {
}
class Response {
}
/**
* 关注公众号:捡田螺的小男孩
*/
abstract class AbstractHandler {
//责任链中的下一个对象
private AbstractHandler nextHandler;
/**
* 责任链的下一个对象
*/
public void setNextHandler(AbstractHandler nextHandler) {
this.nextHandler = nextHandler;
}
/**
* 具体参数拦截逻辑,给子类去实现
*/
public void filter(Request request, Response response) {
doFilter(request, response);
if (getNextHandler() != null) {
getNextHandler().filter(request, response);
}
}
public AbstractHandler getNextHandler() {
return nextHandler;
}
abstract void doFilter(Request filterRequest, Response response);
}
/**
* 安全校验对象
*/
@Component
@Order(2)
//校验顺序排第2
class CheckSecurityFilterObject extends AbstractHandler {
@PostConstruct
public void init() {
System.out.println("2");
}
@Override
public void doFilter(Request request, Response response) {
//invoke Security check
System.out.println("安全调用校验");
}
}
/**
* 黑名单校验对象
*/
@Component
@Order(3)
//校验顺序排第3
class CheckBlackFilterObject extends AbstractHandler {
@PostConstruct
public void init() {
System.out.println("3");
}
@Override
public void doFilter(Request request, Response response) {
//invoke black list check
System.out.println("校验黑名单");
}
}
/**
* 规则拦截对象
*/
@Component
@Order(4)
//校验顺序排第4
class CheckRuleFilterObject extends AbstractHandler {
@PostConstruct
public void init() {
System.out.println("4");
}
@Override
public void doFilter(Request request, Response response) {
//check rule
System.out.println("check rule");
}
}
/**
* 参数校验对象
**/
@Component
@Order(1)
//顺序排第1,最先校验
class CheckParamFilterObject extends AbstractHandler {
@PostConstruct
public void init() {
System.out.println("1");
}
@Override
public void doFilter(Request request, Response response) {
System.out.println("非空参数检查");
}
}
转载:实战!工作中常用到哪些设计模式 (qq.com)
你可能感兴趣的:(架构设计,责任链模式,java,开发语言)