1. In the simple version, multiple filter methods are executed in sequence
import java.util.ArrayList; import java.util.List; public class Test1 { public static void main(String[] args) { Msg msg = new Msg(); msg.setMsg("aaa<aaa>???? + + + + "); FilterChain filterChain = new FilterChain(); filterChain.addFilter(new Filter1()).addFilter(new Filter2()); FilterChain filterChain2 = new FilterChain(); filterChain2.addFilter(new Filter3()).addFilter(new Filter4()); filterChain.addFilter(filterChain2); filterChain.doFilter(msg); System.out.println(msg); } } class Msg { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "Msg{" + "msg='" + msg + '\'' + '}'; } } interface Filter{ void doFilter(Msg s); } class Filter1 implements Filter{ @Override public void doFilter(Msg s) { s.setMsg(s.getMsg().replace("<","[")); } } class Filter2 implements Filter{ @Override public void doFilter(Msg s) { s.setMsg(s.getMsg().replace(">","]")); } } class Filter3 implements Filter{ @Override public void doFilter(Msg s) { s.setMsg(s.getMsg().replace("?" , "..." )); } } class Filter4 implements Filter{ @Override public void doFilter(Msg s) { s.setMsg(s.getMsg().replace("+","-")); } } class FilterChain implements Filter{ private List<Filter> filters = new ArrayList<>(); FilterChain addFilter(Filter filter){ filters.add(filter); return this; } public void doFilter(Msg msg){ for (Filter filter : filters) { filter.doFilter(msg); }}Copy the code
2. The simulation executes the request of each Filter in sequence and then executes the Response of each Filter in reverse order
The key point is that each Filter has a whole chain of references, similar to the recursive process
import java.util.ArrayList; import java.util.List; /** * but I want to execute Filter1, Filter2, Public class Test1 {public static void main(String[] args) {Request Request = new Request(); Response response = new Response(); request.setMsg("==="); response.setMsg("+++"); FilterChain filterChain = new FilterChain(); filterChain.addFilter(new Filter1()).addFilter(new Filter2()); filterChain.doFilter(request,response,filterChain); System.out.println("request = " + request); System.out.println("response = " + response); } } class Request{ private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "Request{" + "msg='" + msg + '\'' + '}'; } } class Response { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "Response{" + "msg='" + msg + '\'' + '}'; } } interface Filter{ boolean doFilter(Request request,Response response,FilterChain filterChain); } class Filter1 implements Filter { @Override public boolean doFilter(Request request, Response response,FilterChain filterChain) { String msg = request.getMsg(); request.setMsg(msg+"ccc"); filterChain.doFilter(request,response,filterChain); String msg1 = response.getMsg(); response.setMsg(msg1+"ddd"); return true; } } class Filter2 implements Filter { @Override public boolean doFilter(Request request, Response response,FilterChain filterChain) { String msg = request.getMsg(); request.setMsg(msg+"aaa"); filterChain.doFilter(request,response,filterChain); String msg1 = response.getMsg(); response.setMsg(msg1+"bbb"); return true; } } class FilterChain implements Filter { private List<Filter> filters = new ArrayList<>(); int index = 0; FilterChain addFilter(Filter filter){ filters.add(filter); return this; } public boolean doFilter(Request request,Response response,FilterChain filterChain){ if(index == filters.size()){ return false; } Filter f = filters.get(index); index = index + 1; return f.doFilter(request,response,filterChain); }}Copy the code