一、 责任链概述
1.1 什么是责任链
责任链将用几个对象接收处理对象,这些对象是 链式
连接的。一个事件先传到第一个对象进行处理,若第一个对象无法处理完成,则第一个对象会将事件再传递到第二个对象,以此类推,若某个对象能够处理完事件,则事件是不一定会继续向后面的对象传递,这个后面再做讨论。
1.2 责任链的应用场景
- 当一个事件的处理有许多的if...else...时,可以将一个个判断处理封装成对象,再将事件发送给这些对象;
- 多个对象处理同一个事件;
- Android中事件分发机制;
1.3 责任链类图
二、 实例
2.1 场景描述
在教师批改大批量试卷时,不会一个教师批改整张试卷。为了更高的效率,每份试卷会经过几个老师批改,每个老师只负责批改那一道题。因此我们可以用责任链模拟这种情况,若一份试卷到第一个老师A这里,A发现她负责的第一道题还没批改,就会进行批改,然后把试卷给另外一位老师,以此类推。
- 事件
试卷- 事件接收处理者
老师
2.2 过程图示
2.3 代码实例
老师会担任事件处理者的位置,老师以链式连接,试卷无论是否批改完成,都会顺着老师这条链传送下去。
- 试卷
public class TestPaper {
//all subject
public static final int SUBJECT_ONE = 1;
public static final int SUBJECT_TWO = 2;
public static final int SUBJECT_THREE = 3;
public static final int SUBJECT_FOUR = 4;
//the hole test paper has four subject,the list save the subject which has been modified
private List<Integer> subjects = new ArrayList<>();
public TestPaper(Integer ... subject){
subjects.addAll(Arrays.asList(subject));
}
/**
* whether the subject has been modified
* @param subject
* @return
*/
public boolean contains(Integer subject) {
boolean contains = false;
for (Integer s : subjects) {
if (subject.equals(s)){
contains = true;
}
}
return contains;
}
/**
* add a modified subject
* @param subject
*/
public void addSubject(Integer subject){
subjects.add(subject);
}
}
- 老师基类——处理事件的基类
public abstract class TeacherBase {
private TestPaper mTestPaper;
private int mSubject;
private TeacherBase mNextTeacher;
/**
* @param subject the subject which the teacher need to check
*/
public TeacherBase(TestPaper testPaper, int subject){
mTestPaper = testPaper;
mSubject = subject;
}
/**
* check the test paper,but don't have to modify it;
*/
public void checkPaper(){
if (!mTestPaper.contains(mSubject)) {
handleRequest();
mTestPaper.addSubject(mSubject);
}
//send the test paper to next teacher
if (mNextTeacher != null) {
mNextTeacher.checkPaper();
}
}
public void setNextTecher(TeacherBase teacher){
mNextTeacher = teacher;
}
/**
* handle the message
*/
public abstract void handleRequest();
}
- 负责不同题目的老师——事件接收类
public class TeacherA extends TeacherBase{
/**
* @param testPaper
* @param subject the subject which the teacher need to check
*/
public TeacherA(TestPaper testPaper, int subject) {
super(testPaper, subject);
}
@Override
public void handleRequest() {
System.out.println("我是A老师,我批改了题目1");
}
}
public class TeacherB extends TeacherBase{
/**
* @param testPaper
* @param subject the subject which the teacher need to check
*/
public TeacherB(TestPaper testPaper, int subject) {
super(testPaper, subject);
}
@Override
public void handleRequest() {
System.out.println("我是B老师,我批改了题目2");
}
}
public class TeacherC extends TeacherBase{
/**
* @param testPaper
* @param subject the subject which the teacher need to check
*/
public TeacherC(TestPaper testPaper, int subject) {
super(testPaper, subject);
}
@Override
public void handleRequest() {
System.out.println("我是C老师,我批改了题目3");
}
}
public class TeacherD extends TeacherBase{
/**
* @param testPaper
* @param subject the subject which the teacher need to check
*/
public TeacherD(TestPaper testPaper, int subject) {
super(testPaper, subject);
}
@Override
public void handleRequest() {
System.out.println("我是D老师,我批改了题目4");
}
}
- 执行
public static void main(String[] args){
//provide a test paper which the second subject has been checked.
TestPaper testPaper = new TestPaper(TestPaper.SUBJECT_TWO);
//each teacher check different test paper.
TeacherA teacherA = new TeacherA(testPaper,TestPaper.SUBJECT_ONE);
TeacherB teacherB = new TeacherB(testPaper,TestPaper.SUBJECT_TWO);
TeacherC teacherC = new TeacherC(testPaper,TestPaper.SUBJECT_THREE);
TeacherD teacherD = new TeacherD(testPaper,TestPaper.SUBJECT_FOUR);
teacherA.setNextTecher(teacherD);
teacherD.setNextTecher(teacherB);
teacherB.setNextTecher(teacherC);
teacherA.checkPaper();
}
out:
我是A老师,我批改了题目1
我是D老师,我批改了题目4
我是C老师,我批改了题目3
由于我们执行时,发出的试卷的第二道题已经批改完成了,所以当试卷传递到B老师时,她不需要处理试卷,直接将试卷传递给下一个老师C。
四、 责任链的分类
上面这个例子中,无论试卷(事件)是否批改完成,都会传递给下一个老师(事件接受者),因此每个老师都会接收到事件,只是做不做批改(处理)而已。这种每个事件接受者都会接收到事件的模式,不是纯责任链。
4.1 纯责任链
纯责任链的模式中,当某个接受者处理
了事件,则会将这个事件拦截
下来,下一个事件接受者是不会接收
到事件的。
4.2 非纯责任链
非纯责任链中,当事件接收者处理
了事件,还可以将事件传递
给下一个事件接收者。
文章参考自:
书籍:HeadFirst 设计模式;
责任链模式:http://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html