标签:
责任链(Chain of Responsibility)模式 :责任链模式是对象的行为模式。使多个对象都有机会处理请求,从而避免请求的发送者和接受者直接的耦合关系。将这些对象连成一条链,沿着这条链传递该请求,直到有一个对象处理它为止。责任链模式强调的是每一个对象及其对下家的引用来组成一条链,利用这种方式将发送者和接收者解耦,类图如下:
package chainOfResp; public abstract class Handler { protected Handler successor; public abstract void handlerRequest(String condition); public Handler getSuccessor() { return successor; } public void setSuccessor(Handler successor) { this.successor = successor; } }
package chainOfResp; public class ConcreteHandler1 extends Handler { @Override public void handlerRequest(String condition) { // 如果是自己的责任,就自己处理,负责传给下家处理 if(condition.equals("ConcreteHandler1")){ System.out.println( "ConcreteHandler1 handled "); return ; }else{ System.out.println( "ConcreteHandler1 passed "); getSuccessor().handlerRequest(condition); } } }
package chainOfResp; public class ConcreteHandler2 extends Handler { @Override public void handlerRequest(String condition) { // 如果是自己的责任,就自己处理,负责传给下家处理 if(condition.equals("ConcreteHandler2")){ System.out.println( "ConcreteHandler2 handled "); return ; }else{ System.out.println( "ConcreteHandler2 passed "); getSuccessor().handlerRequest(condition); } } }
package chainOfResp; public class ConcreteHandlerN extends Handler { /** * 这里假设n是链的最后一个节点必须处理掉 * 在实际情况下,可能出现环,或者是树形, * 这里并不一定是最后一个节点。 * */ @Override public void handlerRequest(String condition) { System.out.println( "ConcreteHandlerN handled"); } }
package chainOfResp; public class Client { public static void main(String[] args) { Handler handler1 = new ConcreteHandler1(); Handler handler2 = new ConcreteHandler2(); Handler handlern = new ConcreteHandlerN(); //链起来 handler1.setSuccessor(handler2); handler2.setSuccessor(handlern); //假设这个请求是ConcreteHandler2的责任 handler1.handlerRequest("ConcreteHandler2"); } }
package chainOfResp.example; public abstract class Handler { protected Handler handler; public abstract boolean approve(double day); public Handler getHandler() { return handler; } public void setHandler(Handler handler) { this.handler = handler; } }
package chainOfResp.example; public class GroupLeader extends Handler { @Override public boolean approve(double day) { if(day<0.5){ System.out.println("小组长审批通过"); return true; }else { System.out.println("小组长传给了他的上司"); return getHandler().approve(day); } } }
package chainOfResp.example; public class ProjectManager extends Handler { @Override public boolean approve(double day) { if(day<2){ System.out.println("项目经理审批通过"); return true; }else { System.out.println("项目经理传给了他的上司"); return getHandler().approve(day); } } }
package chainOfResp.example; public class DepartmentManager extends Handler { @Override public boolean approve(double day) { if(day<5){ System.out.println("部门经理审批通过"); return true; }else { System.out.println("部门经理传给了他的上司"); return getHandler().approve(day); } } }
package chainOfResp.example; public class CEO extends Handler { @Override public boolean approve(double day) { System.out.println("部门经理审批通过"); return true; } }
package chainOfResp.example; public class Client { public static void main(String[] args) { //创建节点 GroupLeader gl = new GroupLeader(); ProjectManager pm = new ProjectManager(); DepartmentManager dm = new DepartmentManager(); CEO ceo = new CEO(); //建立责任链 gl.setHandler(pm); pm.setHandler(dm); dm.setHandler(ceo); //向小组长发出申请,请求审批4天的假期 gl.approve(4D); } }
运行结果:
标签:
原文地址:http://www.cnblogs.com/liujun5020/p/5421309.html