디자인 모드 filter

3164 단어
import java.util.ArrayList; import java.util.List;
public class Main {
        public static void main(String agrs[]) {
                    Request request = new Request();
                    request.requestString = "<      HTML>";
                    Response response = new Response();
                    response.responseString = "resposne :";

                    FilterChain chain = new FilterChain();
                    chain.addFilter(new HtmlFilter());

                    FilterChain chain1 = new FilterChain();
                    chain1.addFilter(new HtmlFilter());
                    chain1.addFilter(new SensitiveFilter());

                    //chain.addFilter(chain1);

                    chain1.doFilter(request, response, chain1);

                    System.out.println("request:" + request.requestString);
                    System.out.println("response:" + response.responseString);
        }

}
interface Filter {
        void doFilter(Request request, Response response, FilterChain chain);

}
class HtmlFilter implements Filter {
        @Override
        public void doFilter(Request request, Response response, FilterChain chain) {
                    request.requestString = request.requestString.replace("<", "[");
                    request.requestString = request.requestString.replace(">", "]");
                    request.requestString += "  html  ";
                    chain.doFilter(request, response, chain);
                    response.responseString += "  html  ";
        }

}
class SensitiveFilter implements Filter {
        @Override
        public void doFilter(Request request, Response response, FilterChain chain) {
                    request.requestString = request.requestString.replace("  ", "mingan");
                    request.requestString += "  sensitive  ";
                    chain.doFilter(request, response, chain);
                    response.responseString += "  sensitive  ";
        }

}
class FilterChain implements Filter {
        private List<Filter> filters = new ArrayList<Filter>();

        public FilterChain addFilter(Filter filter) {
                    if (!filters.contains(filter)) {
                                filters.add(filter);
                    }
                    return this;
        }
        int index = 0;

        @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 Request {
        String requestString;

}
class Response {
        String responseString;

}

좋은 웹페이지 즐겨찾기