[设计模式]职责链模式
来源:互联网 发布:ubuntu u盘 编辑:程序博客网 时间:2024/05/17 08:52
开始写设计模式系列,希望自己可以坚持下来.
第七篇:职责链模式
什么是职责链模式
关于职责链模式GOF是这样定义的:使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间额耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。
简易实现
Talk is cheap,我们来看下最为简易的职责链实现。
抽象的处理者:
package top.huyuxin.ChainofResponsibility;public abstract class Handler { public Handler nextHandler; public abstract void handleRequest(String condition);}
具体的处理者:
package top.huyuxin.ChainofResponsibility;public class FirstHandler extends Handler { @Override public void handleRequest(String condition) { if("first".equals(condition)){ System.out.println("firsthandler capture it!"); }else{ nextHandler.handleRequest(condition); } }}
package top.huyuxin.ChainofResponsibility;public class SecondHandler extends Handler { @Override public void handleRequest(String condition) { if("second".equals(condition)){ System.out.println("secondhandler capture it!"); }else{ nextHandler.handleRequest(condition); } }}
package top.huyuxin.ChainofResponsibility;public class ThirdHandler extends Handler { @Override public void handleRequest(String condition) { if("third".equals(condition)){ System.out.println("thirdhandler capture it!"); }else{ System.out.println("no one can capture it!"); } }}
使用:
package top.huyuxin.ChainofResponsibility;public class Main { public static void main(String[] args) { Handler firstHandler = new FirstHandler(); Handler secondHandler = new SecondHandler(); Handler thirdHandler = new ThirdHandler(); firstHandler.nextHandler=secondHandler; secondHandler.nextHandler=thirdHandler; firstHandler.handleRequest("first"); System.out.println("-----------"); firstHandler.handleRequest("second"); System.out.println("-----------"); firstHandler.handleRequest("third"); System.out.println("-----------"); firstHandler.handleRequest("four"); }}
输出:
firsthandler capture it!-----------secondhandler capture it!-----------thirdhandler capture it!-----------no one can capture it!
这就是简易的职责链,结构特别的简单,但是问题来了,对于稳定运行的代码他的职责链应该是相对固定的,那么难道我们每次使用都要new出每一个处理者并建立职责链吗?我们可以建立一个管理类将这个过程封装起来
package top.huyuxin.ChainofResponsibility;public class HandlerManger { private HandlerManger() { super(); } public static class HandlerManagerInstance{ private static HandlerManger instance=new HandlerManger(); public static HandlerManger getInstance() { return instance; } } private static Handler firstHandler; private static Handler secondHandler; private static Handler thirdHandler; static{ firstHandler = new FirstHandler(); secondHandler = new SecondHandler(); thirdHandler = new ThirdHandler(); } public void handleRequest(String condition){ firstHandler.nextHandler=secondHandler; secondHandler.nextHandler=thirdHandler; firstHandler.handleRequest(condition); }}
调用:
package top.huyuxin.ChainofResponsibility;public class Main { public static void main(String[] args) { HandlerManger handlerManger = HandlerManger.HandlerManagerInstance.getInstance(); handlerManger.handleRequest("first"); System.out.println("-----------"); handlerManger.handleRequest("second"); System.out.println("-----------"); handlerManger.handleRequest("third"); System.out.println("-----------"); handlerManger.handleRequest("four"); }
结果是一致的,。
实战
在实际的项目中请求条件的匹配简单的字符串判断是远远不够的,
我们需要将请求类也抽象出来。
抽象请求类:
package top.huyuxin.ChainofResponsibility.plus;public abstract class AbstrastRequest { //定义统一的请求级别 public static final int FIRST_REQUEST=1; public static final int SECOND_REQUEST=2; public static final int THIRD_REQUEST=3; //可有可无,用于开发者创建请求时传入个性化且必要的初始化参数 private Object object; public AbstrastRequest(Object object){ this.object=object; } //获取对应的参数 public Object getContent(){ return object; } public abstract int getRequestLevel();}
具体的请求:
package top.huyuxin.ChainofResponsibility.plus;import org.omg.CORBA.Request;public class FirstRequest extends AbstrastRequest { public FirstRequest(String name) { super(name); } @Override public int getRequestLevel() { return FIRST_REQUEST; } @Override public String toString() { return "FirstRequest [name=" + getContent() + ", getRequestLevel()=" + getRequestLevel() + "]"; }}
package top.huyuxin.ChainofResponsibility.plus;public class SecondRequest extends AbstrastRequest { public SecondRequest(int initflag) { super(initflag); } @Override public int getRequestLevel() { return SECOND_REQUEST; } @Override public String toString() { return "SecondRequest [initflag=" + getContent() + ", getRequestLevel()=" + getRequestLevel() + "]"; }}
package top.huyuxin.ChainofResponsibility.plus;public class ThirdRequest extends AbstrastRequest { public ThirdRequest(Boolean flag) { super(flag); } @Override public int getRequestLevel() { return THIRD_REQUEST; } @Override public String toString() { return "ThirdRequest [flag=" + false + ", getRequestLevel()=" + getRequestLevel() + "]"; }}
抽象的处理类:
package top.huyuxin.ChainofResponsibility.plus;public abstract class AbstractHandler { protected AbstractHandler nextHandler; public final void handlerRequest(AbstrastRequest request){ if(getHandleLevel()==request.getRequestLevel()){ handle(request); }else { if(nextHandler!=null){ nextHandler.handle(request); }else{ System.out.println("no handler can deal with the request!"); } } } protected abstract int getHandleLevel(); protected abstract void handle(AbstrastRequest request);}
具体的处理类:
package top.huyuxin.ChainofResponsibility.plus;public class FirstHandler extends AbstractHandler { @Override protected int getHandleLevel() { return AbstrastRequest.FIRST_REQUEST; } @Override protected void handle(AbstrastRequest request) { System.out.println("i can deal with the request:"+request.toString()); }}
package top.huyuxin.ChainofResponsibility.plus;public class SecondHandler extends AbstractHandler { @Override protected int getHandleLevel() { return AbstrastRequest.SECOND_REQUEST; } @Override protected void handle(AbstrastRequest request) { System.out.println("i can deal with the request:"+request.toString()); }}
package top.huyuxin.ChainofResponsibility.plus;public class ThirdHandler extends AbstractHandler { @Override protected int getHandleLevel() { return AbstrastRequest.THIRD_REQUEST; } @Override protected void handle(AbstrastRequest request) { System.out.println("i can deal with the request:"+request.toString()); }}
职责链管理器:
package top.huyuxin.ChainofResponsibility.plus;public class HandlerManger { private HandlerManger() { super(); } public static class HandlerManagerInstance{ private static HandlerManger instance=new HandlerManger(); public static HandlerManger getInstance() { return instance; } } private static AbstractHandler firstHandler; private static AbstractHandler secondHandler; private static AbstractHandler thirdHandler; static{ firstHandler = new FirstHandler(); secondHandler = new SecondHandler(); thirdHandler = new ThirdHandler(); } public void handleRequest(AbstrastRequest request){ firstHandler.nextHandler=secondHandler; secondHandler.nextHandler=thirdHandler; firstHandler.handlerRequest(request); }}
调用:
package top.huyuxin.ChainofResponsibility.plus;public class Main { public static void main(String[] args) { HandlerManger handlerManger=HandlerManger.HandlerManagerInstance.getInstance(); FirstRequest firstRequest=new FirstRequest("FirstRequest"); SecondRequest secondRequest=new SecondRequest(2); ThirdRequest thirdRequest=new ThirdRequest(true); handlerManger.handleRequest(firstRequest); handlerManger.handleRequest(secondRequest); handlerManger.handleRequest(thirdRequest); }}
结果:
i can deal with the request:FirstRequest [name=FirstRequest, getRequestLevel()=1]i can deal with the request:SecondRequest [initflag=2, getRequestLevel()=2]i can deal with the request:ThirdRequest [flag=false, getRequestLevel()=3]
我们将请求分离了出来丰富了请求的内容,使得能够在请求体内携带更多的信息。这些信息能够在请求的初始化以及处理类的处理中提供很大的帮助,并且我们将处理请求方法(handle)与请求分发分离(handlerRequest),使得逻辑更加的清晰。
职责链模式是比较简单的行为模式,他和状态模式比起来相似之处很多,但是又截然不同。
参考《Android源码设计模式解析与实战》中第九章–使编程更有灵活性——职责链模式
阅读全文
0 0
- 职责链设计模式
- 【设计模式】职责链
- 设计模式---职责链
- 职责链设计模式
- 职责链设计模式
- 设计模式---职责链模式
- 设计模式-职责链模式
- 设计模式 职责链模式
- 设计模式- 职责链模式
- 设计模式 - 职责链模式
- 设计模式 - 职责链模式
- 设计模式-职责链模式
- 设计模式--职责链模式
- 设计模式-职责链模式
- 设计模式--职责链模式
- 设计模式-职责链模式
- 【设计模式】职责链模式
- 设计模式-职责链模式
- C++中链表的创建、输出、节点删除、节点插入、翻转、清空
- 深浅拷贝与写时拷贝的简单认知
- mysql Community Server 5.7安装教程以及常见错误
- Java 9 新特性之模块化和进程API
- 智慧城市建设概述
- [设计模式]职责链模式
- Redis系列-7.有序集合(zset)结构
- X86 指令速查
- android自定义ViewGroup卫星导航菜单
- dp
- .NET Conf 2017后初尝Xamarin Forms 3.0@Linux
- 列表组件的运用
- 9-24NOIP模拟赛总结
- RMI不支持远程注册(绑定)