责任链设计模式
责任链设计模式:使多个对象都有处理请求的机会,目的使请求的发送者和处理者解耦。将这些对象连成一条链,沿着这条链传递请求,直到有对象处理为止。
具体实现的核心:
一.对定义一个处理请求的方法handleMessage
二.定义下一处理者的方法setNext(Handler _handler)
三.两个供子类实现的抽象方法:
一个是定义级别protected abstract String getHandlerLevel();
一个具体实现的业务方法protected abstract Response echo(Request request);
优点:请求的发送者不必知道处理者,而处理者也不必知道请求者的全貌,使两者解耦。
缺点:一是性能问题,每次请求需要从头遍历,二是调试,使用递归的方法增加调试复杂度
使用场景:J2EE中开发无状态Bean,币种的转换,普通用户和VIP用户的等。
package org.responseChain;public interface Request {public String getHandlerLevel();public Response ehco();;}
package org.responseChain;public class RequestImp implements Request {String level = "0";String request;public RequestImp(String level, String request) {this.level = level;this.request = request;}@Overridepublic String getHandlerLevel() {return level;}@Overridepublic Response ehco() {System.out.println("向:" + level + "发送请求----请求为:" + request);return null;}}
package org.responseChain;public abstract class Handler {private Handler nextHaddler;// 设置下一个处理者public void setNext(Handler _handler) {nextHaddler = _handler;}// 对上 层接口开放的方法public final Response handleMessage(Request request) {Response response = null;// 判断是否是自己处理的级别,是的话就进行处理if (this.getHandlerLevel().equals(request.getHandlerLevel())) {response = this.echo(request);} else {// 如果有下一节点,则交由下一节点处理if (this.nextHaddler != null) {response = nextHaddler.handleMessage(request);} else {System.out.println("找不到请求的处理者");System.out.println();System.out.println();System.out.println();}}return response;}// 子类实现,设置数据链子类的处理级别protected abstract String getHandlerLevel();// 子类实现的业务方法protected abstract Response echo(Request request);}
package org.responseChain;public class ConcreteHandler1 extends Handler {@Overrideprotected String getHandlerLevel() {// TODO Auto-generated method stubreturn Level.LEVEL_1;}@Overrideprotected Response echo(Request request) {// TODO Auto-generated method stubSystem.out.println(request.ehco());System.out.println("ConcreteHandler"+getHandlerLevel()+"处理");System.out.println();System.out.println();System.out.println();return null;}}
package org.responseChain;public class ConcreteHandler2 extends Handler {@Overrideprotected String getHandlerLevel() {// TODO Auto-generated method stubreturn Level.LEVEL_2;}@Overrideprotected Response echo(Request request) {// TODO Auto-generated method stubSystem.out.println(request.ehco());System.out.println("ConcreteHandler" + getHandlerLevel() + "处理");System.out.println();System.out.println();System.out.println();return null;}}
package org.responseChain;public class ConcreteHandler3 extends Handler {@Overrideprotected String getHandlerLevel() {// TODO Auto-generated method stubreturn Level.LEVEL_3;}@Overrideprotected Response echo(Request request) {// TODO Auto-generated method stubSystem.out.println(request.ehco());System.out.println("ConcreteHandler" + getHandlerLevel() + "处理");System.out.println();System.out.println();System.out.println();return null;}}
package org.responseChain;public class SetConcreteHandle {private Handler handler1;private Handler handler2;private Handler handler3;public SetConcreteHandle() {handler1 = new ConcreteHandler1();handler2 = new ConcreteHandler2();handler3 = new ConcreteHandler3();}public final Response handleMessage(Request request){Response response = null;handler1.setNext(handler2);handler2.setNext(handler3);response = handler1.handleMessage(request);return response;}}
package org.responseChain;public class Level {public final static String LEVEL_1 = "1";public final static String LEVEL_2 = "2";public final static String LEVEL_3 = "3";}
package org.responseChain;public class Response {}
package org.responseChain;import java.util.ArrayList;import java.util.Random;public class Client {public static void main(String[] args) {// TODO Auto-generated method stubRandom random = new Random();SetConcreteHandle setConcreteHandle = new SetConcreteHandle();for (int i = 0; i < 5; i++) {String level = String.valueOf(random.nextInt(4));System.out.println("----------------向" + level+ "发送请求----------------");Request request = new RequestImp(level, "请领导加工资");setConcreteHandle.handleMessage(request);}}}