设计模式:Filter过滤器

场景1:
每种Filter进行某种处理,将各个Filter连在一起形成一个FilterChain。当需要往FilterChain中加入新的Filter,或者改变各个Filter的处理顺序时,可以方便的扩展。

1个接口:

public interface Filter {
    public String dofilter(String msg);
}

3种实现类:

public class SensitiveWordFilter implements Filter{

    @Override
    public String dofilter(String msg) {
        return msg.replace("fuck", "f**k").replace("Fuck", "F**k");
    }

}
public class StopWordFilter implements Filter{

    @Override
    public String dofilter(String msg) {
        return msg.replace("and", "").replace("to", "").replace("ing", "").replace("is", "");
    }

}
public class FaceFilter implements Filter{

    @Override
    public String dofilter(String msg) {
        return msg.replace(":(", "^V^");
    }

}

用1个FilterChain连接起来:

public class FilterChain implements Filter{

    List filters = new ArrayList();

    @Override
    public String dofilter(String msg) {
        String r = msg;
        for(Filter f : filters)
            r = f.dofilter(r);
        return r;
    }

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

}

main方法调用:

public class Main {

    public static void main(String[] args){

        String msg = "Today is raining. What a fucking day! :(";

        FilterChain fc = new FilterChain();
        fc.addFilter(new SensitiveWordFilter())
          .addFilter(new StopWordFilter())
          .addFilter(new FaceFilter());

        String result = fc.dofilter(msg);
        System.out.println(msg);
        System.out.println(result);

    }

}

场景2:
客户端==>服务端,参照javax.servlet
设计模式:Filter过滤器_第1张图片
调用流程

2种对象:

public class Request {
    public String requestSTR;
}

public class Response {
    public String responseSTR;
}

1个Filter接口:

public interface Filter {
    public void dofilter(Request request,Response response, FilterChain fc);
}

3种实现类:

public class SensitiveWordFilter implements Filter{

    @Override
    public void dofilter(Request request, Response response, FilterChain fc) {
        request.requestSTR = request.requestSTR.replace("fuck", "f**k").replace("Fuck", "F**k");
        request.requestSTR += "--SensitiveWordFilter()";
        fc.dofilter(request, response, fc);
        response.responseSTR += "--SensitiveWordFilter()";
    }

}
public class StopWordFilter implements Filter{


    @Override
    public void dofilter(Request request, Response response, FilterChain fc) {
        request.requestSTR = request.requestSTR.replace("and", "").replace("to", "").replace("ing", "").replace("is", "");
        request.requestSTR += "--StopWordFilter()";
        fc.dofilter(request, response, fc);
        response.responseSTR += "--StopWordFilter()";
    }

}
public class FaceFilter implements Filter{

    @Override
    public void dofilter(Request request, Response response, FilterChain fc) {
        request.requestSTR = request.requestSTR.replace(":(", "^V^");
        request.requestSTR += "--FaceFilter()";
        fc.dofilter(request, response, fc);
        response.responseSTR += "--FaceFilter()";
    }

}

FilterChain

public class FilterChain implements Filter{

    List filters = new ArrayList();
    int index = 0;

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


    @Override
    public void dofilter(Request request, Response response, FilterChain fc) {
        if(index == filters.size())
            return;
        Filter f = filters.get(index);
        index++;
        f.dofilter(request, response, this);
    }

}

Main:

public class Main {

    public static void main(String[] args){

        Request request = new Request();
        request.requestSTR = "Today is raining. What a fucking day! :(";
        Response response = new Response();
        response.responseSTR = "";

        FilterChain fc = new FilterChain();
        fc.addFilter(new SensitiveWordFilter())
          .addFilter(new StopWordFilter())
          .addFilter(new FaceFilter());

        fc.dofilter(request,response,fc);
        System.out.println(request.requestSTR);
        System.out.println(response.responseSTR);

    }

}

Reference:马士兵java视频,设计模式

你可能感兴趣的:(设计模式)