设计模式:责任链

Java代码   收藏代码
  1. package com.bjsxt.dp.filter;  
  2.   
  3. public class Main {  
  4.     /** 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) {  
  8.         String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";  
  9.         MsgProcessor mp = new MsgProcessor();  
  10.         mp.setMsg(msg);  
  11.         FilterChain fc = new FilterChain();  
  12.         fc.addFilter(new HTMLFilter())  
  13.           .addFilter(new SesitiveFilter())  
  14.           ;  
  15.         FilterChain fc2 = new FilterChain();  
  16.         fc2.addFilter(new FaceFilter());  
  17.           
  18.         fc.addFilter(fc2);  
  19.         mp.setFc(fc);  
  20.         String result = mp.process();  
  21.         System.out.println(result);  
  22.     }  
  23.   
  24. }  


------------------------------------------------------------------ 
Java代码   收藏代码
  1. package com.bjsxt.dp.filter;  
  2.   
  3. public class MsgProcessor {  
  4.     private String msg;  
  5.       
  6.     //Filter[] filters = {new HTMLFilter(), new SesitiveFilter(), new FaceFilter()};  
  7.     FilterChain fc;  
  8.       
  9.     public FilterChain getFc() {  
  10.         return fc;  
  11.     }  
  12.   
  13.     public void setFc(FilterChain fc) {  
  14.         this.fc = fc;  
  15.     }  
  16.   
  17.     public String getMsg() {  
  18.         return msg;  
  19.     }  
  20.   
  21.     public void setMsg(String msg) {  
  22.         this.msg = msg;  
  23.     }  
  24.   
  25.     public String process() {  
  26.           
  27.           
  28.         return fc.doFilter(msg);  
  29.           
  30.           
  31.     }  
  32. }  

------------------------------------------------------------------- 
Java代码   收藏代码
  1. package com.bjsxt.dp.filter;  
  2.   
  3. public interface Filter {  
  4.     String doFilter(String str);  
  5. }  

--------------------------------------------------------------------- 
Java代码   收藏代码
  1. package com.bjsxt.dp.filter;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class FilterChain implements Filter {  
  7.     List<Filter> filters = new ArrayList<Filter>();  
  8.       
  9.     public FilterChain addFilter(Filter f) {  
  10.         this.filters.add(f);  
  11.         return this;  
  12.     }  
  13.       
  14.     public String doFilter(String str) {  
  15.         String r = str;  
  16.         for(Filter f: filters) {  
  17.             r = f.doFilter(r);  
  18.         }  
  19.         return r;  
  20.     }  
  21. }  

Java代码   收藏代码
  1. package com.bjsxt.dp.filter;  
  2.   
  3. public class HTMLFilter implements Filter {  
  4.   
  5.     @Override  
  6.     public String doFilter(String str) {  
  7.         //process the html tag <>  
  8.         String r = str.replace('<''[')  
  9.                    .replace('>'']');  
  10.         return r;  
  11.     }  
  12.   
  13. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值