责任链(Chain of Responsibility)模式的定义:为了避免请求发送者与多个请求处理者耦合在一起,将所有请求的处理者通过前一对象记住其下一个对象的引用而连成一条链;当有请求发生时,可将请求沿着这条链传递,直到有对象处理它为止。
1.职责链模式主要包含以下角色
①抽象处理者:定义一个处理请求的接口,包含抽象处理方法和下一个处理者。
②具体处理者:实现抽象的处理方法,具体的责任处理,判断是否进行下一责任的处理。
③客户类:责任链的创建。
2.模式的具体实现
①抽象处理者
/**
* 抽象处理者
*/
public abstract class Handler {
// 下一个处理者
private Handler next;
/**
* 处理请求
*/
public abstract void handleRequest(String request);
public Handler getNext() {
return next;
}
public void setNext(Handler next) {
this.next = next;
}
}
②具体实现者123
/**
* 具体处理类1
*/
public class ConcreteHandler1 extends Handler{
/**
* 实现处理方法的
*/
@Override
public void handleRequest(String request) {
if(null == request) {
System.out.println("具体处理1完成处理!并中断处理!");
}else {
if(null == this.getNext()) {
System.out.println("具体处理1完成处理!无后续处理,全部处理完成!");
}else {
System.out.println("具体处理1完成处理!并继续具体处理2!");
this.getNext().handleRequest(request);
}
}
}
}
/**
* 具体处理类2
*/
public class ConcreteHandler2 extends Handler{
/**
* 实现处理方法的
*/
@Override
public void handleRequest(String request) {
if(!request.contains("userName")) {
System.out.println("具体处理2完成处理!并中断处理!");
}else {
if(null == this.getNext()) {
System.out.println("具体处理2完成处理!无后续处理,全部处理完成!");
}else {
System.out.println("具体处理2完成处理!并继续具体处理3!");
this.getNext().handleRequest(request);
}
}
}
}
/**
* 具体处理类3
*/
public class ConcreteHandler3 extends Handler{
/**
* 实现处理方法的
*/
@Override
public void handleRequest(String request) {
if(!request.contains("passWord")) {
System.out.println("具体处理3完成处理!并中断处理!");
}else {
if(null == this.getNext()) {
System.out.println("具体处理3完成处理!无后续处理,全部处理完成!");
}else {
System.out.println("具体处理3完成处理!并继续具体处理4!");
this.getNext().handleRequest(request);
}
}
}
}
③责任链的创建者
/**
* 责任链的创建类
*/
public class Client {
// 第一个责任处理者
private Handler firstHandler;
// 当前的责任处理者
private Handler currentHandler;
public Client add(Handler handler) {
if (null == this.firstHandler) {
this.firstHandler = handler;
this.currentHandler = handler;
} else {
this.currentHandler.setNext(handler);
this.currentHandler = handler;
}
return this;
}
public void excutor(String request) {
this.firstHandler.handleRequest(request);
}
}
④测试类
public class Test {
public static void main(String[] args) {
Client client = new Client();
client.add(new ConcreteHandler1())
.add(new ConcreteHandler2())
.add(new ConcreteHandler3());
System.out.println("=====null=====");
client.excutor(null);
System.out.println("=====null=====");
System.out.println("=====空串=====");
client.excutor("");
System.out.println("=====空串=====");
System.out.println("=====userName is not null=====");
client.excutor("userName is not null");
System.out.println("=====userName is not null=====");
System.out.println("=====userName and passWord is not nul=====");
client.excutor("userName and passWord is not null");
System.out.println("=====userName and passWord is not nul=====");
}
}
结果如下:
=====null=====
具体处理1完成处理!并中断处理!
=====null=====
=====空串=====
具体处理1完成处理!并继续具体处理2!
具体处理2完成处理!并中断处理!
=====空串=====
=====userName is not null=====
具体处理1完成处理!并继续具体处理2!
具体处理2完成处理!并继续具体处理3!
具体处理3完成处理!并中断处理!
=====userName is not null=====
=====userName and passWord is not nul=====
具体处理1完成处理!并继续具体处理2!
具体处理2完成处理!并继续具体处理3!
具体处理3完成处理!无后续处理,全部处理完成!
=====userName and passWord is not nul=====