zoukankan      html  css  js  c++  java
  • 抽丝剥茧设计模式- 责任链模式应用场景

      Servlet中应用到责任链模式,下面是仿照实现的代码,请享用!

    package org.design.behavioral.chain.v4;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /*
    需求:
        处理request,response
        请求过来的时候是HtmlFilter处理、SensitiveFilter处理、ScriptFilter处理。
        响应返回的时候是ScriptFilter处理、SensitiveFilter处理、HtmlFilter处理。
     */
    public class ServletFilter {
        public static void main(String[] args) {
            Request request = new Request();
            request.requestStr = "I am request ";
            Response response = new Response();
            response.responseStr = "I am response ";
            FilterChain filterChain = new FilterChain();
            filterChain.add(new HtmlFilter()).add(new SensitiveFilter()).add(new ScriptFilter());
            filterChain.doFilter(request, response);
            System.out.println(request.requestStr);
            System.out.println(response.responseStr);
        }
    }
    
    class Request {
        String requestStr;
    }
    
    class Response {
        String responseStr;
    }
    
    interface Filter {
        boolean doFilter(Request request, Response response, FilterChain filterChain);
    }
    
    class HtmlFilter implements Filter {
        @Override
        public boolean doFilter(Request request, Response response, FilterChain filterChain) {
            request.requestStr = request.requestStr + " | HtmlFilter deal!";
            filterChain.doFilter(request, response);
            response.responseStr = response.responseStr + " | HtmlFilter deal!";
            return true;
        }
    }
    
    class SensitiveFilter implements Filter {
        @Override
        public boolean doFilter(Request request, Response response, FilterChain filterChain) {
            request.requestStr = request.requestStr + " | SensitiveFilter deal!";
            filterChain.doFilter(request, response);
            response.responseStr = response.responseStr + " |  SensitiveFilter deal!";
            return true;
        }
    }
    
    class ScriptFilter implements Filter {
        @Override
        public boolean doFilter(Request request, Response response, FilterChain filterChain) {
            request.requestStr = request.requestStr + " | ScriptFilter deal!";
            filterChain.doFilter(request, response);
            response.responseStr = response.responseStr + " | ScriptFilter deal!";
            return true;
        }
    }
    
    class FilterChain {
        List<Filter> filters = new ArrayList<Filter>();
        int index = 0;
    
        FilterChain add(Filter filter) {
            this.filters.add(filter);
            return this;
        }
    
        public boolean doFilter(Request request, Response response) {
            if (index == filters.size()) return false;
            Filter f = filters.get(index);
            index++;
            f.doFilter(request, response, this);
            return true;
        }
    }
    

      

    感谢阅读到现在,请在留言区提出宝贵的意见!

    更多精彩内容,关注微信公众号:技术严选

  • 相关阅读:
    Spread for Windows Forms快速入门(11)数据筛选
    Spread for Windows Forms高级主题(7)自定义打印的外观
    Spread for Windows Forms高级主题(6)数据绑定管理
    使用WiX制作具有时间限制的安装包
    Spread for Windows Forms高级主题(2)理解单元格类型
    如何添加ActiveReports 6导出PDF时的安全设置和数字签名
    Spread for Windows Forms快速入门(10)绑定到数据库
    当心那些有歧义的命名
    【翻译】会有Silverlight6吗?
    Spread for Windows Forms快速入门(12)数据分组(Outlook风格)
  • 原文地址:https://www.cnblogs.com/wscl/p/15164472.html
Copyright © 2011-2022 走看看