十、责任链模式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
   */
   }
}
 



你可能感兴趣的:(十、责任链模式Chain of Responsibility(行为型模式))