十七、责任链模式
责任链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成
一条链,并沿着这条链传递该请求,直到一个对象处理它为止。 ——摘自《大话设计模式》
Ps:责任处理类都有各自的负责区块,以聚合的方式连成整个的责任链条。
基本结构如下:
①、Request 请求
②、Handler 处理者的超类
Handler 自身的引用,由该属性连接链条
HandleRequest(Request request) 处理请求的方法,该方法用于子类覆写
③、ConcreteHandler 具体的处理者,继承 Handler
HandleRequest(Request request) 覆写超类的处理方法,
内有“区块判断”,本类只处理自己该负责的部分,区块之外的请求会被传递
示例代码:以员工要求请假、加薪,经理们进行审批为例
①、Request #########################################
/** *请求 */public class Request {private String type;//请求的类型private String content; //请求的内容private int number;//请求的数目public Request(String type, String content, int number) {this.type = type;this.content = content;this.number = number;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}}
②、Handler #########################################
/** *管理者 */public abstract class Manager {protected Manager manager; //设置上级protected String name; //管理者名称public Manager(String name){ //要求初始化实例时为名称属性赋值this.name = name;}public void setManager(Manager manager){//设置管理者this.manager = manager;}public abstract void requestApplication(Request request); //响应请求public String getName() {return name;}public void setName(String name) {this.name = name;}public Manager getManager() {return manager;}}
③、ConcreteHandler #########################################
/** *经理,可处理3天以内的假期请求 */public class CommonManager extends Manager {public CommonManager(String name) {super(name);}@Overridepublic void requestApplication(Request request) { //覆写请求处理方法if("请假".equals(request.getType()) && request.getNumber()<=3){ //3天以内的假期请求可自行处理System.out.println(this.name+":"+request.getContent()+",天数:"+request.getNumber()+"\t被批准");}else if(request!=null){this.manager.requestApplication(request); //请求在权限之外,传递请求}}}
/** *总监,可处理6天以内的假期请求 */public class MajorManager extends Manager {public MajorManager(String name) {super(name);}@Overridepublic void requestApplication(Request request) {if("请假".equals(request.getType()) && request.getNumber()<=6){System.out.println(this.name+":"+request.getContent()+",天数:"+request.getNumber()+"\t被批准");}else if(request!=null){manager.requestApplication(request);}}}
/** *总经理, 可处理任意天数的假期请求和薪水调整请求 */public class GeneralManager extends Manager{public GeneralManager(String name) {super(name);}@Overridepublic void requestApplication(Request request) {if("请假".equals(request.getType())){System.out.println(this.name+":"+request.getContent()+",天数:"+request.getNumber()+"\t被批准");}else if("加薪".equals(request.getType()) && request.getNumber()<=500){ //500及500以下的加薪请求会被批准System.out.println(this.name+":"+request.getContent()+","+request.getNumber()+"元\t被批准");}else if("加薪".equals(request.getType()) && request.getNumber()>500){ //500以上的加薪请求会被拒绝System.out.println(this.name+":"+request.getContent()+","+request.getNumber()+"元\t再说吧");}}}
测试方法:
public class Test {public static void main(String[] args) throws Exception {Manager m1 = new CommonManager("欧阳经理");Manager m2 = new MajorManager("司马总监");Manager m3 = new GeneralManager("夏侯总经理");//设置链条,责任次序m1.setManager(m2);m2.setManager(m3);//各种请求Request r1 = new Request("请假","duke要请假",2);Request r2 = new Request("请假","duke要请假",5);Request r3 = new Request("请假","duke要请假",8);Request r4 = new Request("加薪","duke要加薪",500);Request r5 = new Request("加薪","duke要加薪",1000);m1.requestApplication(r1);m1.requestApplication(r2);m1.requestApplication(r3);m1.requestApplication(r4);m1.requestApplication(r5);}}
运行结果:
欧阳经理:duke要请假,天数:2 被批准
司马总监:duke要请假,天数:5 被批准
夏侯总经理:duke要请假,天数:8 被批准
夏侯总经理:duke要加薪,500元 被批准
夏侯总经理:duke要加薪,1000元 再说吧