设计模式之——责任链模式

1 责任链

1 package com.alvin;

2 

3 public interface Filter {

4     public String doFilter(String str);

5 }
1 package com.alvin;

2 

3 public class HtmlFilter implements Filter{

4 

5     public String doFilter(String str) {

6         return str.replace('<', '[').replace('>', ']');

7     }

8 

9 }
 1 package com.alvin;

 2 

 3 public class WordsFilter implements Filter{

 4 

 5     @Override

 6     public String doFilter(String str) {

 7         // TODO Auto-generated method stub

 8         return str.replace("铭感词", "河蟹");

 9     }

10     

11 }
 1 package com.alvin;

 2 

 3 public class MesProcessor {

 4     private String msg;

 5     Filter [] filters = {new HtmlFilter(), new WordsFilter()};

 6 

 7     public String getMsg() {

 8         return msg;

 9     }

10 

11     public void setMsg(String msg) {

12         this.msg = msg;

13     }

14     

15     public String process(){

16         String r = msg;

17         for (Filter f : filters) {

18             r = f.doFilter(r);

19         }

20         return r;

21     }

22 }
 1 package com.alvin;

 2 

 3 public class Main {

 4     public static void main(String[] args) {

 5         String msg = "<script>,:),kdkjfejfid, 敏感词:)";

 6         MesProcessor mp = new MesProcessor();

 7         mp.setMsg(msg);

 8         String result = mp.process();

 9         System.out.println(result);

10     }

11 }

测试结果为:[script],:),kdkjfejfid, 河蟹:)

2 改进数组,采用ArrayList,增加FilterChain类

 1 package com.alvin;

 2 

 3 import java.util.ArrayList;

 4 import java.util.List;

 5 

 6 public class FilterChain {

 7     List<Filter> filters = new ArrayList<>();

 8 

 9     public void addFilter(Filter f) {

10         filters.add(f);

11     }

12 

13     public String doFilter(String str) {

14         for (Filter filter : filters) {

15             str = filter.doFilter(str);

16         }

17         return str;

18     }

19 }

 

修改MesProcessor

 1 package com.alvin;

 2 

 3 public class MesProcessor {

 4     private String msg;

 5     FilterChain filterChain;

 6 

 7     public FilterChain getFilterChain() {

 8         return filterChain;

 9     }

10 

11     public void setFilterChain(FilterChain filterChain) {

12         this.filterChain = filterChain;

13     }

14 

15     public String getMsg() {

16         return msg;

17     }

18 

19     public void setMsg(String msg) {

20         this.msg = msg;

21     }

22 

23     public String process() {

24         return filterChain.doFilter(msg);

25     }

26 }

测试类

 1 package com.alvin;

 2 

 3 public class Main {

 4     public static void main(String[] args) {

 5         String msg = "<script>,:),kdkjfejfid, 敏感词:)";

 6         MesProcessor mp = new MesProcessor();

 7         mp.setMsg(msg);

 8         FilterChain filterChain = new FilterChain();

 9         filterChain.addFilter(new HtmlFilter());

10         filterChain.addFilter(new WordsFilter());

11         mp.setFilterChain(filterChain);

12         String result = mp.process();

13         System.out.println(result);

14     }

15 }

测试结果:[script],:),kdkjfejfid, 河蟹:)

 

你可能感兴趣的:(责任链模式)