十、责任链模式Chain of Responsibility(行为型模式)

单向的责任链

interface Filter {
   String doFilter(String str);
}
class FilterChain implements Filter {
   List<Filter> filters = new ArrayList<>();
   public FilterChain addFilter(Filter f) {
      this.filters.add(f);
      return this;
   }
   public String doFilter(String str) {
      String r = str;
      for (Filter f : filters) {
        r = f.doFilter(r);
      }
      return r;
   }
}
class HTMLFilter implements Filter {
   @Override
   public String doFilter(String str) {
      // 处理<>
      String r = str.replace('<', '[').replace('>', ']');
      return r;
   }
}
class SesitiveFilter implements Filter {
   @Override
   public String doFilter(String str) {
      // 处理敏感
      String r = str.replace("被就业", "就业").replace("敏感", "");
      return r;
   }
}
class FaceFilter implements Filter {
   @Override
   public String doFilter(String str) {
      return str.replace(":)", "^U^");
   }
}
class MsgProcessor {
   String msg;
   /*
    * Filter[] filters={new HTMLFilter(),new SesitiveFilter(),new
    * FaceFilter()};
    */
   FilterChain filterChain;

   public FilterChain getFilterChain() {
      return filterChain;
   }
   public void setFilterChain(FilterChain filterChain) {
      this.filterChain = filterChain;
   }
   public String getMsg() {
      return msg;
   }
   public void setMsg(String msg) {
      this.msg = msg;
   }
   public String process() {
      /*
       * // 处理<> String r = msg.replace('<', '[').replace('>', ']');
       *
       * //处理敏感 r=r.replace("被就业","就业").replace("敏感", "");
       *
       * //.........
       */
      /* String r=new HTMLFilter().doFilter(msg); */
      /*
       * String r=msg; for(Filter f:filters){ r=f.doFilter(r); }
       */
      String r = filterChain.doFilter(msg);
      return r;
   }
}
import java.util.ArrayList;
import java.util.List;

public class Main {
   public static void main(String[] args) {

      String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大伙";
      MsgProcessor mp = new MsgProcessor();
      mp.setMsg(msg);
      FilterChain chain = new FilterChain().addFilter(new HTMLFilter())
           .addFilter(new SesitiveFilter());
      FilterChain chain2 = new FilterChain().addFilter(new FaceFilter());
      chain.addFilter(chain2);

      mp.setFilterChain(chain);
      String result = mp.process();
      System.out.println(result);
   }
}

来回责任链
interface Filter {
   void doFilter(Request request, Response response, FilterChain chain);
}
class FilterChain implements Filter {
   List<Filter> filters = new ArrayList<>();
   int index = 0;
   public FilterChain addFilter(Filter f) {
      this.filters.add(f);
      return this;
   }
   @Override
   public void doFilter(Request request, Response response, FilterChain chain) {
      if (index == filters.size())
        return;
      Filter f = filters.get(index);
      index++;
      f.doFilter(request, response, chain);
   }
}

class SesitiveFilter implements Filter {
   @Override
   public void doFilter(Request request, Response response, FilterChain chain) {
      // 处理<>
      request.request = request.request.replace("被就业", "就业").replace("敏感", "") + "---SesitiveFilter";
      chain.doFilter(request, response, chain);
      response.response += "---SesitiveFilter";
   }
}

class HTMLFilter implements Filter {
   @Override
   public void doFilter(Request request, Response response, FilterChain chain) {
      // 处理<>
      request.request = request.request.replace('<', '[').replace('>', ']')+ "--HTMLFilter";
      chain.doFilter(request, response, chain);
      response.response += "--HTMLFilter";
   }
}
class FaceFilter implements Filter {
   @Override
   public void doFilter(Request request, Response response, FilterChain chain) {
      // 处理<>
      request.request = request.request.replace(":)", "^U^")+ "---FaceFilter";
      chain.doFilter(request, response, chain);
      response.response += "---FaceFilter";
   }
}
class Request {
   String request;
   public String getRequest() {
      return request;
   }
   public void setRequest(String request) {
      this.request = request;
   }
}
class Response {
   String response;
   public String getResponse() {
      return response;
   }
   public void setResponse(String response) {
      this.response = response;
   }
}
import java.util.ArrayList;
import java.util.List;
public class Main {
   public static void main(String[] args) {
      String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大伙";
      Request request = new Request();
      request.request = msg;
      Response response = new Response();
      response.response = "response";
      FilterChain chain = new FilterChain().addFilter(new HTMLFilter()).addFilter(new SesitiveFilter()).addFilter(new FaceFilter());
      chain.doFilter(request, response, chain);
      System.out.println(request.request);
      System.out.println(response.response);
  /*
   * FaceFilter response---FaceFilter---SesitiveFilter--HTMLFilter
   */
   }
}
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值