欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

也说责任链模式应用

程序员文章站 2022-07-02 13:26:31
...
背景:刚看到一个同事用了一堆ifelse嵌套来判断是否执行最后业务逻辑,感觉很纠结,就想到能不能用责任链模式来处理这类问题。顺便记录下来备后面回顾思考
问题类似如下:
Java代码  收藏代码
    if() {  
      if() {  
        if() {  
         ....  
           doBusiness();  
        }  
      }  
    }  



于是想到了责任链,直接贴代码如下:

处理接口,对外请求处理接口
    package com.shuidexiongdi.chain;  
      
    public interface Handler {  
          
        /** 
         * 执行请求 
         * @return 一般的责任链不要求一定返回boolean,这是针对具体的例子设计的责任链模式 
         */  
        public boolean doRequest();  
          
        public void setNextHandler(Handler handler);  
      
    }  


抽象一个模板类

    package com.shuidexiongdi.chain;  
      
    import org.apache.log4j.Logger;  
      
      
    public abstract class AbstractHandler implements Handler {  
          
        private static final Logger logger = Logger.getLogger(AbstractHandler.class);  
      
        Handler nextHandler;  
      
        public boolean doRequest() {  
            logger.info(this.getClass().getName() + "start..");  
            boolean flag = this.mydo();  
            logger.info(this.getClass().getName() + "flag: " + flag);  
            //如果当前逻辑正确并且需要下一个校验,则继续,否则返回当前校验  
            if(flag && this.getNextHandler() != null) {  
                return this.getNextHandler().doRequest();  
            }  
            return flag;  
        }  
      
        public Handler getNextHandler() {  
            return this.nextHandler;  
        }  
      
        public void setNextHandler(Handler handler) {  
            this.nextHandler = handler;  
        }  
          
        /** 
         * 用于校验某个逻辑是否正确 
         * @return 
         */  
        public abstract boolean mydo();  
      
    }  



    package com.shuidexiongdi.chain;  
      
    public class OneHandler extends AbstractHandler implements Handler {  
          
        /** 
         * 用于校验某个逻辑是否正确 
         * @return 
         */  
        public boolean mydo() {  
            return true;  
        }  
          
    }  




    package com.shuidexiongdi.chain;  
      
    public class TwoHandler extends AbstractHandler implements Handler {  
      
        /** 
         * 用于校验某个逻辑是否正确 
         * @return 
         */  
        public boolean mydo() {  
            return true;  
        }  
      
    }  




    package com.shuidexiongdi.chain;  
      
    public class ThirdHandler extends AbstractHandler implements Handler {  
      
        /** 
         * 用于校验某个逻辑是否正确 
         * @return 
         */  
        public boolean mydo() {  
            return true;  
        }  
      
    }  




    package com.shuidexiongdi.chain;  
      
    /** 
     * 加入某个校验返回出错的逻辑 
     * @author dengjianquan-ghq 
     * 
     */  
    public class WrongHandler extends AbstractHandler implements Handler {  
      
        /** 
         * 用于校验某个逻辑是否正确 
         * @return 
         */  
        public boolean mydo() {  
            return false;  
        }  
      
    }  



客户端:
    package com.shuidexiongdi.chain;  
      
    public class Client {  
          
        public static void main(String[] args) {  
            doTest1();  
            doTest2();  
            doTest3();  
            doTest4();  
        }  
          
        private static void doTest1() {  
            Handler handler = new OneHandler();  
            Handler handler2 = new TwoHandler();  
            Handler handler3 = new ThirdHandler();  
            Handler wrongHandler = new WrongHandler();  
            handler.setNextHandler(handler2);  
            handler2.setNextHandler(handler3);  
            handler3.setNextHandler(wrongHandler);  
            System.out.println(handler.doRequest());  
        }  
          
        private static void doTest2() {  
            Handler handler = new OneHandler();  
            Handler handler2 = new TwoHandler();  
            Handler handler3 = new ThirdHandler();  
            handler.setNextHandler(handler2);  
            handler2.setNextHandler(handler3);  
            System.out.println(handler.doRequest());  
        }  
          
        private static void doTest3() {  
            Handler handler = new OneHandler();  
            Handler handler2 = new TwoHandler();  
            Handler handler3 = new ThirdHandler();  
            handler.setNextHandler(handler2);  
            handler2.setNextHandler(handler3);  
            handler3.setNextHandler(null);  
            System.out.println(handler.doRequest());  
        }  
          
        private static void doTest4() {  
            Handler handler = new OneHandler();  
            Handler handler2 = new TwoHandler();  
            Handler handler3 = new ThirdHandler();  
            Handler wrongHandler = new WrongHandler();  
            handler.setNextHandler(handler2);  
            handler2.setNextHandler(wrongHandler);  
            wrongHandler.setNextHandler(handler3);  
            System.out.println(handler.doRequest());  
        }  
      
    }  


相关标签: chain ifelse