您的位置:首页 > 编程语言 > Java开发

JAVA责任链模式-过滤器原理

2016-07-06 16:11 483 查看
[java]
view plain
copy





package test;  
  
public class Test {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        String msg = "大家好:),<script>,敏感,网络授课没感觉,因为看不见大伙";  
        /** 
         * 需求:网站需要对我提交的内容进行过滤 
         */  
          
        MagProcessor magProcessor = new MagProcessor();  
        magProcessor.setMsg(msg);  
        String msgdb = magProcessor.process();  
    }  
  
}  



[java]
view plain
copy





package test;  
  
public class MagProcessor {  
    String msg;  
  
    public String getMsg() {  
        return msg;  
    }  
    public void setMsg(String msg) {  
        this.msg = msg;  
    }  
    public String process(){  
        //process html tag  
        String r = msg.replace("<", "[")  
                    .replace(">", "]");  
        //process the sensitive words  
        r=r.replace("被就业", "就业");  
          
          
        return null;  
    }  
}  

[java]
view plain
copy





通过MagProcessor 的process对信息进行过滤,一旦发生新的需求,我们不得不修养这个方法,这就违背了对修改关闭,对扩张开发的设计思想,那么怎么办呢?  
请看第二节。  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public class MsgProcessor {  
    private String msg;  
      
    Filter[] filters = {new HTMLFilter(), new SesitiveFilter(), new FaceFilter()};  
      
    public FilterChain getFc() {  
        return fc;  
    }  
  
    public void setFc(FilterChain fc) {  
        this.fc = fc;  
    }  
  
    public String getMsg() {  
        return msg;  
    }  
  
    public void setMsg(String msg) {  
        this.msg = msg;  
    }  
  
    public String process() {  
        String msg  = "";  
          
        for(Filter f:filters){  
                 r=f.doFilters(r);  
                }  
        return r;  
    }  
}  

通过这样的方式,我们分化了各个校验规则,提高了可扩展性,下面请考虑另外一个问题,如果有其他的链条,如何拼接呢?

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public class MsgProcessor {  
    private String msg;  
      
    //Filter[] filters = {new HTMLFilter(), new SesitiveFilter(), new FaceFilter()};  
    FilterChain fc;  
      
    public FilterChain getFc() {  
        return fc;  
    }  
  
    public void setFc(FilterChain fc) {  
        this.fc = fc;  
    }  
  
    public String getMsg() {  
        return msg;  
    }  
  
    public void setMsg(String msg) {  
        this.msg = msg;  
    }  
  
    public String process() {  
          
          
        return fc.doFilter(msg);  
          
          
    }  
}  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
import java.util.ArrayList;  
import java.util.List;  
  
public class FilterChain implements Filter {  
    List<Filter> filters = new ArrayList<Filter>();  
      
    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;  
    }  
}  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public class Main {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";  
        MsgProcessor mp = new MsgProcessor();  
        mp.setMsg(msg);  
        FilterChain fc = new FilterChain();  
        fc.addFilter(new HTMLFilter())  
          .addFilter(new SesitiveFilter())  
          ;  
        FilterChain fc2 = new FilterChain();  
        fc2.addFilter(new FaceFilter());  
          
        fc.addFilter(fc2);  
        mp.setFc(fc);  
        String result = mp.process();  
        System.out.println(result);  
    }  
  
}  

我们要实现链条的拼接,其实我们想想,一个Filter链条本身就是一个Filter,所以我们使用FilterChain来模拟Filter链条,并且实现了Filter接口,
这样我们的Filterchain不仅可以添加一个个Filter,还能直接添加Filter链条,现在的问题是如何让我们的Filter也可以处理返回的信息呢?

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public interface Filter {  
    void doFilter(Request request, Response response, FilterChain chain);  
}  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public class Main {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";  
        Request request = new Request();  
        request.setRequestStr(msg);  
        Response response = new Response();  
        response.setResponseStr("response");  
        FilterChain fc = new FilterChain();  
        fc.addFilter(new HTMLFilter())  
          .addFilter(new SesitiveFilter())  
          ;  
          
        fc.doFilter(request, response, fc);  
        System.out.println(request.getRequestStr());  
        System.out.println(response.getResponseStr());  
    }  
  
}  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
import java.util.ArrayList;  
import java.util.List;  
  
public class FilterChain implements Filter {  
    List<Filter> filters = new ArrayList<Filter>();  
    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);  
    }  
}  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public class HTMLFilter implements Filter {  
  
      
  
    @Override  
    public void doFilter(Request request, Response response, FilterChain chain) {  
        //process the html tag <>  
        request.requestStr = request.requestStr.replace('<', '[')  
                   .replace('>', ']') + "---HTMLFilter()";  
        chain.doFilter(request, response, chain);  
        response.responseStr += "---HTMLFilter()";  
    }  
  
}  

[java]
view plain
copy





package com.bjsxt.dp.filter;  
  
public class SesitiveFilter implements Filter {  
  
      
  
    @Override  
    public void doFilter(Request request, Response response, FilterChain chain) {  
        request.requestStr = request.requestStr.replace("被就业", "就业")  
         .replace("敏感", "") + "---SesitiveFilter()";  
        chain.doFilter(request, response, chain);  
        response.responseStr += "---SesitiveFilter()";  
      
    }  
      
      
  
}  

要想让我们的Filter可以处理返回信息,很自然的想到让一个Filter去调用另外一个Filter,等到最后一个Filter返回,其余的也逐个返回,这里要完成这一过程,增加了

一个参数FilterChain,FilterChain里面记录了当前过滤器索引,巧妙的完成了一个Filter调用另外一个Filter的需求
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: