@w1992wishes
2018-03-13T06:13:30.000000Z
字数 6205
阅读 1221
设计模式 行为型模式
本文的结构如下:
现在的网络小说套路大概是这样的,小喽啰惹上主角,主角霸气侧漏,打跑了;小喽啰找来门中厉害的亲爹,亲爹上场,过了两招一看打不过,一起跑路;但这口气咽不下,一咬牙找来爷爷(一般是长老或者宗主),宗主那当然厉害了,主角打不过,狼狈逃命,下定功夫苦加修炼又或者奇遇连连,功力大增,杀上山门怒杀全宗,报仇雪恨。

这种自己打不过,找爸爸,爸爸打不过找爷爷的模式就是责任链模式。
很多情况下,在一个软件系统中可以处理某个请求的对象不止一个,比如上面跟主角打架,小喽啰、小喽啰他爸、小喽啰他爷都可以跟主角打架,打不打得过不管(处不处理不管),主角沿着这条链进行传递,这条链就称为职责链。
职责链可以是一条直线、一个环或者一个树形结构,最常见的职责链是直线型,即沿着一条单向的链来传递请求。链上的每一个对象都是请求处理者,责任链模式可以将请求的处理者组织成一条链,并让请求沿着链传递,由链上的处理者对请求进行相应的处理,客户端无须关心请求的处理细节以及请求的传递,只需将请求发送到链上即可,实现请求发送者和请求处理者解耦。
责任链模式定义如下:
责任链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。责任链模式是一种对象行为型模式。
责任链模式结构的核心在于引入了一个抽象处理者,责任链模式的UML类图如下:

在责任链模式结构图中包含如下几个角色:
要形成链式调用,关键在于每一个处理者中有一个下一个处理者的引用。
抽象处理者是核心,典型代码如下:
public abstract class Handler {protected Handler successor;public void setSuccessor(){this.successor = successor;}public Handler getSuccessor(){return successor;}public abstract void handler(String condition);}
具体处理者继承抽象处理者,主要作用有两个:第一是处理请求,如果能够处理就自己处理了;第二是转发请求,如果该请求超出了当前处理者类的权限,可以将该请求转发给下家。具体处理者类典型代码如下:
public class ConcreteHandlerA extends Handler {public void handler(String condition) {if ("conditionA".equals(condition)){System.out.println("处理请求");}else {System.out.println("转发请求");successor.handler(condition);}}}public class ConcreteHandlerB extends Handler {public void handler(String condition) {if ("conditionB".equals(condition)){System.out.println("处理请求");}else {System.out.println("转发请求");successor.handler(condition);}}}
责任链模式并不创建职责链,职责链的创建工作必须由系统的其他部分来完成,一般是在使用该职责链的客户端中创建职责链。
以引言中的找主角打架为例。
public class Protagonist {private int forceValue;//主角武力值private String name;private int age;public Protagonist(String name, int age, int forceValue) {this.name = name;this.age = age;this.forceValue = forceValue;}public void setForceValue(int forceValue) {this.forceValue = forceValue;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public int getForceValue() {return forceValue;}public String getName() {return name;}public int getAge() {return age;}}public class FightHandler {public void fightWith(Protagonist protagonist){if (protagonist.getForceValue() < 100){//主角武力值小于100,自己上selfFighting(protagonist);}else if (protagonist.getForceValue() < 200){//找爸爸fatherFighting(protagonist);}else if(protagonist.getForceValue() < 400){grandfatherFighting(protagonist);} else {togetherFighting(protagonist);}}private void selfFighting(Protagonist protagonist){//todoSystem.out.println("我打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}private void fatherFighting(Protagonist protagonist){//todoSystem.out.println("我爸打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}private void grandfatherFighting(Protagonist protagonist){//todoSystem.out.println("我爷打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}private void togetherFighting(Protagonist protagonist){//todoSystem.out.println("我们一起打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}}
看到这段代码,相信都会发现一些问题存在:

先定义抽象处理者:
public abstract class FightHandler {protected FightHandler successor;public void setSuccessor(FightHandler successor){this.successor = successor;}public abstract void fightWith(Protagonist protagonist);}
几个具体处理者:
public class SelfFightHandler extends FightHandler {public void fightWith(Protagonist protagonist) {if (protagonist.getForceValue() < 100){//todoSystem.out.println("我打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}else {successor.fightWith(protagonist);}}}public class FatherFightHandler extends FightHandler{public void fightWith(Protagonist protagonist) {if (protagonist.getForceValue() < 200){//todoSystem.out.println("我爸打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}else {successor.fightWith(protagonist);}}}public class GrandfatherFightHandler extends FightHandler {public void fightWith(Protagonist protagonist) {if (protagonist.getForceValue() < 400){//todoSystem.out.println("我爷打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}else {successor.fightWith(protagonist);}}}public class TogetherFightHandler extends FightHandler {public void fightWith(Protagonist protagonist) {if (protagonist.getForceValue() > 400){//todoSystem.out.println("我们一起打得" + protagonist.getName() +"屁滚尿流,哭爹喊娘");}else {successor.fightWith(protagonist);}}}
客户端测试:
public class Client {public static void main(String[] args) {FightHandler selfHandler = new SelfFightHandler();FightHandler fatherHandler = new FatherFightHandler();FightHandler grandfatherHandler = new GrandfatherFightHandler();FightHandler togetherHandler = new TogetherFightHandler();//创建职责链selfHandler.setSuccessor(fatherHandler);fatherHandler.setSuccessor(grandfatherHandler);grandfatherHandler.setSuccessor(togetherHandler);//主角Protagonist zxf = new Protagonist("张小凡", 12, 88);selfHandler.fightWith(zxf);Protagonist qf = new Protagonist("乔峰", 33, 188);selfHandler.fightWith(qf);Protagonist zbj = new Protagonist("猪八戒", 500, 288);selfHandler.fightWith(zbj);}}
这时新增一个具体处理者只需要继承抽象处理者即可,由于链的创建过程由客户端负责,因此增加新的具体处理者类对原有类库无任何影响,无须修改已有类的源代码,符合“开闭原则”。
责任链模式可分为纯的责任链模式和不纯的责任链模式两种:
一个纯的责任链模式要求:
在一个不纯的责任链模式中则允许:
纯的责任链模式的实际例子很难找到,一般看到的例子均是不纯的责任链模式的实现。
责任链模式的主要优点如下:
责任链模式的主要缺点如下:
在以下情况下可以考虑使用责任链模式:
责任链设计模式(Chain of Responsibility)的应用有:Java Web中的过滤器链、Struts2中的拦截器栈。