package com.macrotea.responzchain.entity; public class Request { private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
package com.macrotea.responzchain.entity; public class Response { private String reply; public String getReply() { return reply; } public void setReply(String reply) { this.reply = reply; } }
package com.macrotea.responzchain.interfazz; import com.macrotea.responzchain.chain.FilterChain; import com.macrotea.responzchain.entity.Request; import com.macrotea.responzchain.entity.Response; public interface Filter { public void doFilter(Request request,Response response,FilterChain chain); }
package com.macrotea.responzchain.impl; import com.macrotea.responzchain.chain.FilterChain; import com.macrotea.responzchain.entity.Request; import com.macrotea.responzchain.entity.Response; import com.macrotea.responzchain.interfazz.Filter; public class HTMLFilter implements Filter { @Override public void doFilter(Request request, Response response,FilterChain chain) { request.setContent(request.getContent()+"-->HTMLFilter"); /*让链调用下一个过滤器*/ chain.doFilter(request, response,chain); response.setReply(response.getReply()+"-->HTMLFilter"); } }
package com.macrotea.responzchain.impl; import com.macrotea.responzchain.chain.FilterChain; import com.macrotea.responzchain.entity.Request; import com.macrotea.responzchain.entity.Response; import com.macrotea.responzchain.interfazz.Filter; public class HarmonyFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.setContent(request.getContent()+"-->HarmonyFilter"); /*让链调用下一个过滤器*/ chain.doFilter(request, response,chain); response.setReply(response.getReply()+"-->HarmonyFilter"); } }
package com.macrotea.responzchain.chain; import java.util.ArrayList; import java.util.List; import com.macrotea.responzchain.entity.Request; import com.macrotea.responzchain.entity.Response; import com.macrotea.responzchain.interfazz.Filter; public class FilterChain implements Filter{ private List<Filter> filters; private int index=0; public FilterChain() { super(); filters = new ArrayList<Filter>(); } public FilterChain appendFilter(Filter filter) { filters.add(filter); return this; } public FilterChain removeFilter(Filter filter) { filters.remove(filter); return this; } @Override public void doFilter(Request request, Response response,FilterChain chain) { if(index==filters.size())return; Filter filter=filters.get(index); index++; /*具体实现类的调用*/ filter.doFilter(request, response, chain); } }
package com.macrotea.responzchain.test; import com.macrotea.responzchain.chain.FilterChain; import com.macrotea.responzchain.entity.Request; import com.macrotea.responzchain.entity.Response; import com.macrotea.responzchain.impl.HTMLFilter; import com.macrotea.responzchain.impl.HarmonyFilter; public class Mainer { public static void main(String[] args) { Request request =new Request(); request.setContent("这个是请求的内容"); Response response=new Response(); response.setReply("这个是响应的内容"); FilterChain chain=new FilterChain(); chain.appendFilter(new HTMLFilter()).appendFilter(new HarmonyFilter()); chain.doFilter(request, response, chain); System.out.println("经过FilterChain后请求的内容: "+request.getContent()); System.out.println("经过FilterChain后响应的内容: "+response.getReply()); } }
//输出: 经过FilterChain后请求的内容: 这个是请求的内容-->HTMLFilter-->HarmonyFilter 经过FilterChain后响应的内容: 这个是响应的内容-->HarmonyFilter-->HTMLFilter