责任链模式
定义:使多个对象都有机会处理请求,从而避免了请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。
首先来看一段代码:
public void test(int i, Request request){ if(i==1){ Handler1.response(request); }else if(i == 2){ Handler2.response(request); }else if(i == 3){ Handler3.response(request); }else if(i == 4){ Handler4.response(request); }else{ Handler5.response(request); } }
会存在代码臃肿, 耦合度高的问题
责任连模式的结构
- 抽象处理类:抽象处理类中主要包含一个指向下一处理类的成员变量nextHandler和一个处理请求的方法handRequest,handRequest方法的主要主要思想是,如果满足处理的条件,则有本处理类来进行处理,否则由nextHandler来处理。
- 具体处理类:具体处理类主要是对具体的处理逻辑和处理的适用条件进行实现。
等级类:
public class Level { int level = 0; public Level(int level) { this.level = level; } int getLevel() { return level; } //比较等级 boolean above(Level level) { if(this.level >= level.getLevel()) { return true; }else{ return false; } } }
请求和相应:
public class Request { Level level; public Request(Level level) { System.out.println("请求开始"); this.level = level; } public Level getRequestLevel() { return this.level; } }
public class Response { Request request; // 这里只在是为了简单模拟处理请求返回相应 public Response(Request request) { System.out.println("相应结束"); this.request = request; } }
抽象处理类:
public abstract class Handler { private Handler nextHandler; public void setNextHandler(Handler handler) { nextHandler = handler; } public Response handlerRequest(Request request) { Response response = null; if(this.getHandlerLevel().above(request.getRequestLevel())) { response = this.doResponse(request); } else { if(this.nextHandler != null) { response = this.nextHandler.handlerRequest(request); } else { System.out.println("没有相应"); } } return response; } public abstract Response doResponse(Request request); public abstract Level getHandlerLevel(); }
定义三个具体处理类:
// 设置等级为1 public class InterceptorHandler1 extends Handler { @Override public Response doResponse(Request request) { System.out.println("InterceptorHandler1 start response..."); Response response = new Response(request); return response; } @Override public Level getHandlerLevel() { return new Level(1); } }
// 设置等级为2 public class InterceptorHandler2 extends Handler { @Override public Response doResponse(Request request) { System.out.println("InterceptorHandler2 start response..."); Response response = new Response(request); return response; } @Override public Level getHandlerLevel() { return new Level(2); } }
// 设置等级为4 public class InterceptorHandler3 extends Handler { @Override public Response doResponse(Request request) { System.out.println("InterceptorHandler3 start response..."); Response response = new Response(request); return response; } @Override public Level getHandlerLevel() { return new Level(4); } }
测试类:
public class Test { public static void main(String[] args) { InterceptorHandler1 ih1 = new InterceptorHandler1(); InterceptorHandler2 ih2 = new InterceptorHandler2(); InterceptorHandler3 ih3 = new InterceptorHandler3(); ih1.setNextHandler(ih2); ih2.setNextHandler(ih3); ih1.handlerRequest(new Request(new Level(1))); System.out.println("-------------------------"); ih1.handlerRequest(new Request(new Level(3))); System.out.println("-------------------------"); ih1.handlerRequest(new Request(new Level(5))); } }
结果: