The project source address: https: //github.com/ggb2312/JavaNotes/tree/master/design-pattern (design patterns relevant code and notes)
1. Definitions
Receiving a request to create a chain of this request object
2. Applicable scene
- One or more cooperative processing among the processing requires a plurality of object requests
Application examples: struts interceptors, servlet filter, Dubbo the Filter, Mybatis in the Plugin
3. relevant design patterns
Chain of Responsibility pattern and status mode
- Who the Chain of Responsibility pattern of each object does not specify a process object is a set order, and the responsibility of the chain element by the client, until it is processed to a chain of responsibility, or the end of the entire chain of responsibility.
- State model is to make each object knows who the next target processing yes.
Example 4. Mode
Background : Mu lesson course line network at the time of release, to go through the approval of a two people, a man Notes check, another person is to check the video, we will encounter this situation in which oa system, a approval grade level. This is a chain.
(1) Course category
public class Course {
private String name;
private String article;
private String video;
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 getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
@Override
public String toString() {
return "Course{" +
"name='" + name + '\'' +
", article='" + article + '\'' +
", video='" + video + '\'' +
'}';
}
}
(2) approval by the abstract class
/**
* 批准者
*/
public abstract class Approver {
/** 责任链的核心就是在类里面包含了一个自己同样类型的一个对象 */
protected Approver approver;
public void setNextApprover(Approver approver) {
this.approver = approver;
}
public abstract void deploy(Course course) ;
}
(3) Notes approver
public class ArticleApprover extends Approver{
@Override
public void deploy(Course course) {
if (StringUtils.isNoneEmpty(course.getArticle())) {
System.out.println(course.getName() + "含有手记,批准");
if (approver != null) {
approver.deploy(course);
}
} else {
System.out.println(course.getName() + "不含有手记,不批准,流程结束");
return;
}
}
}
Approver (4) Video
public class VideoApprover extends Approver {
@Override
public void deploy(Course course) {
if (StringUtils.isNoneEmpty(course.getVideo())) {
System.out.println(course.getName() + "含有视频,批准");
if (approver != null) {
approver.deploy(course);
}
} else {
System.out.println(course.getName() + "不含有视频,不批准,流程结束");
return;
}
}
}
(5) Testing
public class Test {
public static void main(String[] args) {
Approver articleApprover = new ArticleApprover();
Approver videoApprover = new VideoApprover();
Course course = new Course();
course.setName("Java设计模式精讲");
course.setArticle("Java设计模式精讲的手记");
course.setVideo("Java设计模式精讲的视频");
/** 先通过手记的审核才交给视频的审核,视频的审核过了才能进行发布上线 */
articleApprover.setNextApprover(videoApprover);
articleApprover.deploy(course);
}
}
Test Results:
The advantages and disadvantages
advantage:
- The sender and receiver request (processing request) Decoupling
- The chain of responsibility can be dynamically combined
Disadvantages:
- Chain of responsibility is too long or the processing time is too long, affecting performance
- The chain of responsibility there may be excessive
6. -JDK1.7 source and extended chain of responsibility pattern frame
javax.servlet.Filter的doFilter、FilterChain
SpringSecurity