职责链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。由于英文翻译的不同,职责链模式又称为责任链模式,它是一种对象行为型模式。
从击鼓传花谈起
击鼓传花是一种热闹而又紧张的饮酒游戏。在酒宴上宾客依次坐定位置,由一人击鼓,击鼓的地方与传花的地方是分开的,以示公正。开始击鼓时,花束就开始依次传递,鼓声一落,如果花束在某人手中,则该人就得饮酒。
击鼓传花便是责任链模式的应用。责任链可能是一条直线、一个环链或者一个树结构的一部分。
抽象处理者(Handler)角色:定义出一个处理请求的接口。如果需要,接口可以定义出一个方法,以设定和返回对下家的引用。这个角色通常由一个抽象类或接口实现。
具体处理者(ConcreteHandler)角色:具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。
public abstract class Handler { protected Handler successor; public void setSuccessor(Handler successor) { this.successor=successor; } public abstract void handleRequest(String request); } public class ConcreteHandler extends Handler { public void handleRequest(String request) { if(请求request满足条件) { ...... //处理请求; } else { this.successor.handleRequest(request); //转发请求 } } }代码二、
public class Client { public static void main(String args[]) { Leader objDirector,objManager,objGeneralManager,objViceGeneralManager; objDirector=new Director("王明"); objManager=new Manager("赵强"); objGeneralManager=new GeneralManager("李波"); objViceGeneralManager=new ViceGeneralManager("肖红"); objDirector.setSuccessor(objManager); objManager.setSuccessor(objViceGeneralManager); objViceGeneralManager.setSuccessor(objGeneralManager); LeaveRequest lr1=new LeaveRequest("张三",2); objDirector.handleRequest(lr1); LeaveRequest lr2=new LeaveRequest("李四",5); objDirector.handleRequest(lr2); LeaveRequest lr3=new LeaveRequest("王五",15); objDirector.handleRequest(lr3); LeaveRequest lr4=new LeaveRequest("赵六",45); objDirector.handleRequest(lr4); } }抽象处理角色
public abstract class Leader { protected String name; protected Leader successor; public Leader(String name) { this.name=name; } public void setSuccessor(Leader successor) { this.successor=successor; } public abstract void handleRequest(LeaveRequest request); }具体处理者角色
public class Director extends Leader { public Director(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<3) { System.out.println("主任" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。"); } else { if(this.successor!=null) { this.successor.handleRequest(request); } } } }
public class GeneralManager extends Leader { public GeneralManager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<30) { System.out.println("总经理" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。"); } else { System.out.println("莫非" + request.getLeaveName() + "想辞职,居然请假" + request.getLeaveDays() + "天。"); } } }
public class ViceGeneralManager extends Leader { public ViceGeneralManager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<20) { System.out.println("副总经理" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。"); } else { if(this.successor!=null) { this.successor.handleRequest(request); } } } }
public class Manager extends Leader { public Manager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<10) { System.out.println("经理" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。"); } else { if(this.successor!=null) { this.successor.handleRequest(request); } } } }请求
public class LeaveRequest { private String leaveName; private int leaveDays; public LeaveRequest(String leaveName,int leaveDays) { this.leaveName=leaveName; this.leaveDays=leaveDays; } public void setLeaveName(String leaveName) { this.leaveName = leaveName; } public void setLeaveDays(int leaveDays) { this.leaveDays = leaveDays; } public String getLeaveName() { return (this.leaveName); } public int getLeaveDays() { return (this.leaveDays); } }
try { …… } catch(ArrayIndexOutOfBoundsException e1) { …… } catch(ArithmeticException e2) { …… } catch(IOException e3) { …… } finally { …… }