[设计模式]职责链模式

开始写设计模式系列,希望自己可以坚持下来.
第七篇:职责链模式

什么是职责链模式

关于职责链模式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源码设计模式解析与实战》中第九章–使编程更有灵活性——职责链模式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值