一、定义与类型:
》为请求创建一个接收此次请求对象的链。这个链条中,每个元素是一个对象。
》类型:行为型;
二、适用场景:
》一个请求的处理需要多个对象当中的一个或几个协作处理。
三、优点:
》请求的发送者和接收者(请求的处理)解耦。
》责任链可以动态组合。如果复杂一点,把责任链的顺序和是否需要这个责任链做成外部的配置,则责任链可以动态组合
四、缺点:
》责任链太长或者处理时间过长,影响性能;
》责任链有可能过多。
五、相关设计模式:
》责任链模式和状态模式:责任链模式中各个对象并不指定下一个处理者是谁,只有在客户端也就是test类中,
来设定链条的元素和顺序,直到被某个对象结束或者整个链条结束。而状态模式中,每个对象要知道自己的
下一个处理对象是谁。
六、代码实践:
以网上发布课程为例。一个课程需要有手记和视频才能发布,缺少其中一个则不能发布。
如下,会在test中,进行修改,完成不同的case测试。
package pattern.behaviral.chainofresponsibility;
/**
* Created by Administrator on 2019/9/1.
*/
public class Course {
private String name;
private String article;
private String videoUrl;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArticle() {
return article;
}
public void setArticle(String article) {
this.article = article;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
@Override
public String toString() {
return "Course{" +
"name='" + name + '\'' +
", article='" + article + '\'' +
", videoUrl='" + videoUrl + '\'' +
'}';
}
}
package pattern.behaviral.chainofresponsibility;
/**
* Created by Administrator on 2019/9/1.
*/
public abstract class Approver {
//责任链模式关键的一个写法,类内一个属性为自己
protected Approver approver;//批准者
public void setNextApprover(Approver approver){
this.approver = approver;
}
public abstract void deploy(Course course);
}
package pattern.behaviral.chainofresponsibility;
/**
* Created by Administrator on 2019/9/1.
*/
public class ArticleApprover extends Approver {
public void deploy(Course course) {
if (course.getArticle()!= null){
System.out.println(course.getName()+"含有手记,批准");
if (approver != null){
approver.deploy(course);
}
}else {
System.out.println(course.getName()+"不含有手记,不批准");
}
}
}
package pattern.behaviral.chainofresponsibility;
/**
* Created by Administrator on 2019/9/1.
*/
public class VideoApprover extends Approver {
public void deploy(Course course) {
if (!course.getVideoUrl().equals(null)){
System.out.println(course.getName()+"含有视频,批准");
if (approver != null){
approver.deploy(course);
}
}else {
System.out.println(course.getName()+"不含有视频,不批准");
}
}
}
package pattern.behaviral.chainofresponsibility;
/**
* Created by Administrator on 2019/9/1.
*/
public class Test {
public static void main(String[] args) {
ArticleApprover articleApprover = new ArticleApprover();
VideoApprover videoApprover = new VideoApprover();
Course course = new Course();
course.setName("java设计模式精讲--By Geely");
//注意,可以将如下设置的set方法中注释掉一个,
//来模拟缺少其中一个内容时,课程发布状况的情形
//此处默认两者都有,可以成功发布
course.setArticle("java 设计模式精讲的手记");
course.setVideoUrl("java设计模式精讲的视频");
articleApprover.setNextApprover(videoApprover);
articleApprover.deploy(course);
}
}
"D:\Program Files\Java\jdk1.8.0_102\bin\java" "-javaagent:D:\InteliijIDea\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar=56543:D:\InteliijIDea\IntelliJ IDEA 2017.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;E:\JavaDesignMode\target\classes" pattern.behaviral.chainofresponsibility.Test
java设计模式精讲--By Geely含有手记,批准
java设计模式精讲--By Geely含有手记视频,批准
Process finished with exit code 0
如果如上test类中,注释掉一行,如下,测试结果为:
package pattern.behaviral.chainofresponsibility;
/**
* Created by Administrator on 2019/9/1.
*/
public class Test {
public static void main(String[] args) {
ArticleApprover articleApprover = new ArticleApprover();
VideoApprover videoApprover = new VideoApprover();
Course course = new Course();
course.setName("java设计模式精讲--By Geely");
//注意,可以将如下设置的set方法中注释掉一个,
//来模拟缺少其中一个内容时,课程发布状况的情形
//此处默认两者都有,可以成功发布
//course.setArticle("java 设计模式精讲的手记");
course.setVideoUrl("java设计模式精讲的视频");
articleApprover.setNextApprover(videoApprover);
articleApprover.deploy(course);
}
}
"D:\Program Files\Java\jdk1.8.0_102\bin\java" "-javaagent:D:\InteliijIDea\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar=57145:D:\InteliijIDea\IntelliJ IDEA 2017.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;E:\JavaDesignMode\target\classes" pattern.behaviral.chainofresponsibility.Test
java设计模式精讲--By Geely不含有手记,不批准
Process finished with exit code 0
七、源码解读:
javax.servelet类中 的过滤器 Filter doFilter()方法 FillterChain
MockFilterChain 是spring中为了测试用的
LoggerContextFilter doFilter()方法,类似于deploy()
SpringSecurity中的大量的责任链模式