zoukankan      html  css  js  c++  java
  • 责任链模式-- chainResponsibility

    责任链模式在Struts中用的很多。

    代码:

      1. 创建两个基本类  

    package pr.cgl.chainResponsibility;

    /**
    * Created by LL on 2015/8/27.
    */
    public class Request {
    public String requestStr;

    public String getRequestStr() {
    return requestStr;
    }

    public void setRequestStr(String requestStr) {
    this.requestStr = requestStr;
    }
    }

    package pr.cgl.chainResponsibility;

    /**
    * Created by LL on 2015/8/27.
    */
    public class Response {
    public String responseStr;

    public String getResponseStr() {
    return responseStr;
    }

    public void setResponseStr(String responseStr) {
    this.responseStr = responseStr;
    }
    }

    2. 创建Filter接口
    package pr.cgl.chainResponsibility;

    /**
    * Created by LL on 2015/8/27.
    */
    public interface Filter {
    void doFilter(Request request, Response response, FilterChain fc);
    }
    3. 创建Filter接口的实现类  
    package pr.cgl.chainResponsibility;

    /**
    * Created by LL on 2015/8/27.
    */
    public class HTMLFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain fc) {
    request.requestStr += "--HTMLFilter";
    fc.doFilter(request, response, fc);
    response.responseStr += "--HTMLFilter";
    }
    }

    package pr.cgl.chainResponsibility;
    /**
    * Created by LL on 2015/8/27.
    */
    public class SensitiveFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain fc) {
    request.requestStr += "--SensitiveFilter";
    fc.doFilter(request, response, fc);
    response.responseStr += "--SensitiveFilter";
    }
    }

    4. 创建过滤器链FilterChain
    package pr.cgl.chainResponsibility;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by LL on 2015/8/27.
    */
    public class FilterChain implements Filter {

    List<Filter> filters = new ArrayList<Filter>();
    int i = 0;

    public List<Filter> getFilters() {
    return filters;
    }

    public void setFilters(List<Filter> filters) {
    this.filters = filters;
    }

    public FilterChain addFilter(Filter filter){
    filters.add(filter);
    return this;
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain fc) {
    if(i == filters.size()) return;
    Filter filter = filters.get(i);
    i++;
    filter.doFilter(request, response, fc);
    }
    }
    红色部分是责任链的关键。
    5.测试
      
    package pr.cgl.chainResponsibility;

    /**
    * Created by LL on 2015/8/27.
    */
    public class Test1 {
    public static void main(String[] args) {
    Request request = new Request();
    request.setRequestStr("cgl and xf ");
    Response response = new Response();
    response.setResponseStr("xf and response");
    HTMLFilter htmlFilter = new HTMLFilter();
    SensitiveFilter sensitiveFilter = new SensitiveFilter();
    FilterChain fc = new FilterChain();
    fc.addFilter(htmlFilter).addFilter(sensitiveFilter);
    fc.doFilter(request, response, fc);
    System.out.println(request.getRequestStr());
    System.out.println(response.getResponseStr());
    }
    }
    输出:
      cgl and xf --HTMLFilter--SensitiveFilter
      xf and response--SensitiveFilter--HTMLFilter
  • 相关阅读:
    hihoCoder#1037 : 数字三角形(DP)
    Android安全机制
    Java五道输出易错题解析(进来挑战下)
    【Mail】JavaMail介绍及发送邮件(一)
    【Mail】搭建邮件服务器(LAMP+Postfix+Dovcot+PostfixAdmin+Roundcubemail)
    【Mail】telnet收发邮件过程
    【Mail】邮件的基础知识和原理
    【Java】JavaWeb权限管理
    【Java】JDBC连接数据库
    【Mysql】权限管理
  • 原文地址:https://www.cnblogs.com/cglWorkBook/p/4764285.html
Copyright © 2011-2022 走看看