- 属于行为型设计模式
- 优点:实现了请求者与处理者的代码分离,可以自由组合工作流程,类与类直接松耦合
- 缺点:以链的形式在对象间传递消息,可能会影响处理的速度
- 责任链模式应用在 Spring MVC 的初始化过程中
今天通过一个面试流程来讲解一下责任链模式
public class Applicant {
private Interviewer interviewer;
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void setInterviewer(Interviewer interviewer) {
this.interviewer = interviewer;
}
public Interviewer getInterviewer() {
return interviewer;
}
}
public abstract class Interviewer {
public String name;
Interviewer next;
public Interviewer(String name) {
this.name = name;
}
public void setNext(Interviewer interviewer) {
this.next = interviewer;
}
public abstract void handleApplicant(Applicant applicant);
}
// 初试
public class First extends Interviewer {
public First(String name) {
super(name);
}
@Override
public void handleApplicant(Applicant applicant) {
System.out.println("I am " + name + " interviewer");
if (applicant.getScore() < 5) {
System.out.println("The First Interview failed");
} else {
System.out.println("Enter the next interview");
if (this.next != null) {
this.next.handleApplicant(applicant);
}
}
}
}
// 复试
public class Second extends Interviewer {
public Second(String name) {
super(name);
}
@Override
public void handleApplicant(Applicant applicant) {
System.out.println("I am " + name + " interviewer");
if (applicant.getScore() < 5) {
System.out.println("The Second Interview failed");
} else {
System.out.println("You've already passed all the interviews");
}
}
}
public class Main {
public static void main(String[] args) {
Applicant applicant = new Applicant();
applicant.setScore(7);
Interviewer interviewer1 = new First("stalary");
Interviewer interviewer2 = new Second("claire");
interviewer1.setNext(interviewer2);
interviewer1.handleApplicant(applicant);
System.out.println("-------------------");
Applicant loser = new Applicant();
loser.setScore(1);
interviewer1.handleApplicant(loser);
}
}
// 测试结果
I am stalary interviewer
Enter the next interview
I am claire interviewer
You've already passed all the interviews
-------------------
I am stalary interviewer
The First Interview failed