设计模式之责任链模式
Posted duheng-biu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之责任链模式相关的知识,希望对你有一定的参考价值。
实现:
链条和节点:
public class Process implements Filter { private List<Filter> filters = new ArrayList<>(); int index = 0; public void add(Filter filter) { filters.add(filter); } // 调用filter的方法 @Override public void doFilter(Request request, Reponse reponse, Process process) { if (index == filters.size()) return; Filter fc = filters.get(index); index++; fc.doFilter(request, reponse, process); } } 处理结点: public class SenstiveFilter implements Filter { @Override public void doFilter(Request request, Reponse reponse,Process process) { request.request = request.request.replaceAll("不想学习","爱学习").concat("----》SenstiveFilter"); process.doFilter(request, reponse, process); reponse.reponse = reponse.reponse.concat("----》SenstiveFilter"); } } public class ScriptFilter implements Filter { @Override public void doFilter(Request request,Reponse reponse,Process process) { request.request = request.request.replaceAll("<","[").replaceAll(">", "]").concat("----》ScriptFilter"); process.doFilter(request, reponse, process); reponse.reponse = reponse.reponse.concat("----》ScriptFilter"); } }
解析:
以上是关于设计模式之责任链模式的主要内容,如果未能解决你的问题,请参考以下文章