责任链或过滤器模式2-使用过滤链

[java]  view plain copy
  1. //过滤器接口  
  2. public interface Filter {  
  3.     public String doFilter(String str);  
  4. }  
[java]  view plain copy
  1. //HTML的过滤器  
  2. public class HTMLFilter implements Filter{  
  3.     @Override  
  4.     public String doFilter(String str) {  
  5.         if(str==null)return "";  
  6.         str=str.replace("<""[");  
  7.         str=str.replace(">""]");  
  8.         return str;  
  9.     }  
  10. }  
[java]  view plain copy
  1. //敏感词的过滤器  
  2. public class SensitiveFilter implements Filter{  
  3.     @Override  
  4.     public String doFilter(String str) {  
  5.         if(str==null)return "";  
  6.         str=str.replace("敏感""mingan");  
  7.         return str;  
  8.     }  
  9. }  
[java]  view plain copy
  1. //过滤链,把要使用的过滤器放在集合当中进行依次处理  
  2. public class FilterChain implements Filter{  
  3.     private List<Filter> list=new ArrayList<Filter>();  
  4.     public FilterChain addfilter(Filter filter){  
  5.         list.add(filter);  
  6.         return this;  
  7.     }  
  8.     public String doFilter(String str){  
  9.         System.out.println("#################");  
  10.         if(str==null)return "";  
  11.         for(Filter filter : list){  
  12.             str=filter.doFilter(str);  
  13.         }  
  14.         return str;  
  15.     }  
  16. }  
[java]  view plain copy
  1. //字符串过滤的处理类  
  2. public class Process {  
  3.     private String str;  
  4.     private FilterChain filterchain;  
  5.     //字符串过滤的处理方法  
  6.     public String process(){  
  7.         if(filterchain==nullreturn str;  
  8.         //使用在client类中添加的过滤器在过滤链进行处理  
  9.         return filterchain.doFilter(str);  
  10.     }  
  11.       
  12.     public String getStr() {  
  13.         return str;  
  14.     }  
  15.     public void setStr(String str) {  
  16.         this.str = str;  
  17.     }  
  18.     public FilterChain getFilterchain() {  
  19.         return filterchain;  
  20.     }  
  21.   
  22.     public void setFilterchain(FilterChain filterchain) {  
  23.         this.filterchain = filterchain;  
  24.     }  
  25. }  
[java]  view plain copy
  1. public class Client {  
  2.     public static void main(String[] args) {  
  3.         String str="这是我的一个敏感链接<a>优酷</a>";  
  4.         FilterChain  fc=new FilterChain();  
  5. //      fc.addfilter(new HTMLFilter()).addfilter(new SensitiveFilter());  
  6.         fc.addfilter(new HTMLFilter());  
  7.           
  8.         FilterChain fc1=new FilterChain();  
  9.         fc1.addfilter(new SensitiveFilter());  
  10.         fc.addfilter(fc1);//在一个过滤链上面再加上一个过滤链  
  11.           
  12.         Process pro=new Process();  
  13.         pro.setStr(str);  
  14.         pro.setFilterchain(fc);  
  15.         str=pro.process();  
  16.         System.out.println(str);  
  17.     }  
  18.     /** 
  19.      * 运行结果: 
  20.      * ################# 
  21.      * ################# 
  22.      * 这是我的一个mingan链接[a]优酷[/a] 
  23.      */  
  24. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值