责任链模式
案例
一个BBS系统,对每个人发的评论等进行检查,如敏感信息等。
版本一 简单面向对象
主类:Main
1 package com.laolang.dp.ChainFfResponsibility; 2 3 import java.util.logging.Level; 4 import java.util.logging.Logger; 5 6 import com.laolang.dp.ChainFfResponsibility.filter.MsgProcessor; 7 8 /** 9 * 责任链模式 <br /> 10 * 案例: <br /> 11 * 一个BBS系统,对每个人发的评论等进行检查,如敏感信息等 12 * 13 * @author laolang2016 14 * 15 */ 16 public class Main { 17 18 public static Logger log = Logger.getLogger(Main.class.toString()); 19 20 static { 21 log.setLevel(Level.INFO); 22 } 23 24 public static void main(String[] args) { 25 log.info("责任链模式"); 26 27 String msg = "敏感信息<script></script>大学生被就业了"; 28 System.out.println("原信息:" + msg); 29 30 MsgProcessor msgProcessor = new MsgProcessor(); 31 msgProcessor.setMsg(msg); 32 String result = msgProcessor.processStr(); 33 System.out.println(result); 34 35 } 36 37 }
字符串处理类:MsgProcessor
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 /** 4 * 字体串处理类 5 * 6 * @author laolang2016 7 * 8 */ 9 public class MsgProcessor { 10 11 public MsgProcessor() { 12 super(); 13 } 14 15 /** 16 * 返回已处理的字符串 17 * 18 * @return 19 */ 20 // TODO 21 public String processStr() { 22 String result = msg; 23 24 result = handleHtmlTag(msg); 25 26 result = handleSensitiveWords(result); 27 28 return result; 29 } 30 31 /** 32 * 过滤HTML标记 33 * 34 * @param str 35 * @return 36 */ 37 private String handleHtmlTag(String str) { 38 return str.replaceAll("<", "[").replaceAll(">", "]"); 39 } 40 41 /** 42 * 处理敏感词 43 * 44 * @param str 45 * @return 46 */ 47 private String handleSensitiveWords(String str) { 48 return str.replaceAll("被就业", "***"); 49 } 50 51 public String getMsg() { 52 return msg; 53 } 54 55 public void setMsg(String msg) { 56 this.msg = msg; 57 } 58 59 /** 60 * 待处理字符串 61 */ 62 private String msg; 63 64 }
运行结果
版本二 过滤器
主类:Main
1 package com.laolang.dp.ChainFfResponsibility; 2 3 import java.util.logging.Level; 4 import java.util.logging.Logger; 5 6 import com.laolang.dp.ChainFfResponsibility.filter.MsgProcessor; 7 8 /** 9 * 责任链模式 <br /> 10 * 案例: <br /> 11 * 一个BBS系统,对每个人发的评论等进行检查,如敏感信息等 12 * 13 * @author laolang2016 14 * 15 */ 16 public class Main { 17 18 public static Logger log = Logger.getLogger(Main.class.toString()); 19 20 static { 21 log.setLevel(Level.INFO); 22 } 23 24 public static void main(String[] args) { 25 log.info("责任链模式"); 26 27 String msg = "敏感信息<script></script>大学生被就业了"; 28 System.out.println("原信息:" + msg); 29 30 MsgProcessor msgProcessor = new MsgProcessor(); 31 msgProcessor.setMsg(msg); 32 String result = msgProcessor.processStr(); 33 System.out.println(result); 34 35 } 36 37 }
过滤器接口:WordFilter
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 public interface WordFilter { 4 5 public String doFilter( String str ); 6 }
HTML标记过滤实现:
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 import java.util.logging.Level; 4 import java.util.logging.Logger; 5 6 7 8 /** 9 * HTML 过滤器 10 * 11 * @author laolang2016 12 * 13 */ 14 public class HtmlTagFilter implements WordFilter { 15 16 public static Logger log = Logger.getLogger(HtmlTagFilter.class.toString()); 17 18 static { 19 log.setLevel(Level.INFO); 20 } 21 22 @Override 23 public String doFilter(String str) { 24 log.info("处理HTML标记"); 25 return str.replaceAll("<", "[").replaceAll(">", "]"); 26 } 27 28 }
敏感词过滤实现:SensitiveWordsFilter
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 import java.util.logging.Level; 4 import java.util.logging.Logger; 5 6 7 /** 8 * 9 * 敏感词过滤器 10 * 11 * @author laolang2016 12 * 13 */ 14 public class SensitiveWordsFilter implements WordFilter { 15 16 public static Logger log = Logger.getLogger(SensitiveWordsFilter.class.toString()); 17 18 static { 19 log.setLevel(Level.INFO); 20 } 21 22 @Override 23 public String doFilter(String str) { 24 log.info("处理敏感词"); 25 return str.replaceAll("被就业", "***"); 26 } 27 28 }
敏感信息处理类:MsgProcessor
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 /** 4 * 字体串处理类 5 * 6 * @author laolang2016 7 * 8 */ 9 public class MsgProcessor { 10 11 public MsgProcessor() { 12 super(); 13 } 14 15 /** 16 * 返回已处理的字符串 17 * 18 * @return 19 */ 20 // TODO 21 public String processStr() { 22 String result = msg; 23 24 25 for( WordFilter filter : wordFilters ){ 26 result = filter.doFilter(result); 27 } 28 29 return result; 30 } 31 32 public String getMsg() { 33 return msg; 34 } 35 36 public void setMsg(String msg) { 37 this.msg = msg; 38 } 39 40 /** 41 * 待处理字符串 42 */ 43 private String msg; 44 45 /** 46 * 敏感词过滤器数组 47 */ 48 private WordFilter[] wordFilters = { new HtmlTagFilter() , new SensitiveWordsFilter() }; 49 50 }
版本三 加入过滤器链
过滤器链:FilterChan
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * Created by Administrator on 2017/10/21. 8 */ 9 public class FilterChain implements WordFilter { 10 11 public FilterChain() { 12 wordFilters = new ArrayList<WordFilter>(); 13 } 14 15 /** 16 * 添加过滤器 17 * @param wordFilter 18 */ 19 public FilterChain addFilter( WordFilter wordFilter ){ 20 wordFilters.add(wordFilter); 21 return this; 22 } 23 24 @Override 25 public String doFilter(String str) { 26 String result = str; 27 for( WordFilter filter : wordFilters ){ 28 result = filter.doFilter(result); 29 } 30 return result; 31 } 32 33 private List<WordFilter> wordFilters; 34 }
敏感信息处理类:MsgProcessor
1 package com.laolang.dp.ChainFfResponsibility.filter; 2 3 /** 4 * 字体串处理类 5 * 6 * @author laolang2016 7 * 8 */ 9 public class MsgProcessor { 10 11 public MsgProcessor() { 12 super(); 13 } 14 15 /** 16 * 返回已处理的字符串 17 * 18 * @return 19 */ 20 public String processStr() { 21 return filterChain.doFilter(msg); 22 } 23 24 public String getMsg() { 25 return msg; 26 } 27 28 public void setMsg(String msg) { 29 this.msg = msg; 30 } 31 32 /** 33 * 待处理字符串 34 */ 35 private String msg; 36 37 public FilterChain getFilterChain() { 38 return filterChain; 39 } 40 41 public void setFilterChain(FilterChain filterChain) { 42 this.filterChain = filterChain; 43 } 44 45 /** 46 * 敏感词过滤器数组 47 */ 48 // private WordFilter[] wordFilters = { new HtmlTagFilter() , new SensitiveWordsFilter() }; 49 50 // 过滤器链 51 private FilterChain filterChain; 52 53 }
主类:Main
1 package com.laolang.dp.ChainFfResponsibility; 2 3 import com.laolang.dp.ChainFfResponsibility.filter.FilterChain; 4 import com.laolang.dp.ChainFfResponsibility.filter.HtmlTagFilter; 5 import com.laolang.dp.ChainFfResponsibility.filter.MsgProcessor; 6 import com.laolang.dp.ChainFfResponsibility.filter.SensitiveWordsFilter; 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 10 /** 11 * Created by Administrator on 2017/10/21. 12 */ 13 public class Main { 14 15 private static final Logger log = LoggerFactory.getLogger(Main.class); 16 17 public static void main(String[] args) { 18 log.info("责任链模式"); 19 20 String msg = "敏感信息<script></script>大学生被就业了"; 21 System.out.println("原信息:" + msg); 22 23 MsgProcessor msgProcessor = new MsgProcessor(); 24 msgProcessor.setMsg(msg); 25 26 // 添加过滤器链 27 FilterChain filterChain = new FilterChain(); 28 filterChain.addFilter( new HtmlTagFilter()).addFilter( new SensitiveWordsFilter()); 29 30 // 添加其它过滤器链 31 // FilterChain filterChain2 = new FilterChain(); 32 // filterChain.addFilter( new HtmlTagFilter()).addFilter( new SensitiveWordsFilter()); 33 34 msgProcessor.setFilterChain(filterChain); 35 String result = msgProcessor.processStr(); 36 System.out.println(result); 37 } 38 }