------- android培训、java培训、期待与您交流! ----------
责任链设计模式: 都说面向对象挺难的,所以我专门请教了朋友一下关于如何学习面向对象的方法,朋友建议我看一下设计模式方面的东西,于是我就开始了学习设计模式,下面是我学习的一些心得:首先是责任链设计模式:我理解的责任链就是将一些需要处理的事件装到一个容器里,要被处理的事件从该容器里穿过之后,出来之后就已经完成了一串的处理过程! 像这样的例子我首先想到的就是过滤器,下面是一个小例子: //实现给出一字符串我们将其中的敏感词汇,和一些html信息进行处理 //定义filter接口,面向接口编程有比较好的扩展性 package com.bj.filter; public interface Filter { void doFilter(Request request,Response response,FilterChain chain); } //定义处理链条的类 package com.bj.filter; import java.util.ArrayList; public class FilterChain implements Filter{ ArrayList<Filter>filters=new ArrayList<Filter>(); int index=0; public FilterChain addFilter(Filter f){ filters.add(f); return this; } @Override public void doFilter(Request request, Response response, FilterChain chain) { // TODO Auto-generated method stub if(index==filters.size())return; Filter f=filters.get(index); f.doFilter(request,response,chain); index++; } } //定义几个filter的实现类, package com.bj.filter; //处理html信息的类 public class HtmlFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { // TODO Auto-generated method stub request.requestStr=request.getRequestStr().replace("<", "[") .replace(">", "]"); chain.doFilter(request,response,chain); response.setReponseStr("hahhaha"); } } //处理敏感词汇的类 package com.bj.filter; public class SensitiveFilter implements Filter{ @Override public void doFilter(Request request,Response response,FilterChain chain) { // TODO Auto-generated method stub request.requestStr=request.getRequestStr().replace("敏感", "haha"); chain.doFilter(request, response, chain); response.getReponseStr().replace("haha", "hehe"); } } //定义请求类 package com.bj.filter; public class Request { String requestStr; public String getRequestStr() { return requestStr; } public void setRequestStr(String requestStr) { this.requestStr = requestStr; } } //定义应答类 package com.bj.filter; public class Response { String reponseStr; public String getReponseStr() { return reponseStr; } public void setReponseStr(String reponseStr) { this.reponseStr = reponseStr; } } //定义处理方式的类 package com.bj.filter; import java.util.ArrayList; import com.bj.filter.*; public class MsgProcessor { private String msg; // Filter[] filters={new HtmlFilter(),new SensitiveFilter()}; private FilterChain fc; public FilterChain getFc() { return fc; } // public void setFc(FilterChain fc) { // this.fc = fc; // } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String process(String str){ String r = null; // r=fc.doFilter(str); return r; } public void setFc(FilterChain fc2) { // TODO Auto-generated method stub this.fc=fc2; } } //下面是测试类 package com.bj.filter; import java.util.ArrayList; public class Main { public static void main(String[] args) { String msg="大记号:)<sad>敏感"; Request request=new Request(); request.setRequestStr(msg); Response response=new Response(); response.setReponseStr("response"); FilterChain fc=new FilterChain(); fc.addFilter(new HtmlFilter()); fc.doFilter(request, response, fc); System.out.println(request); System.out.println(response); } } 上边的程序开始执行后会将给出的字符串中的敏感词汇给替换掉。 ------- android培训、java培训、期待与您交流! ----------