Design Pattern-responsibility chain pattern

Source: Internet
Author: User

1. responsibility chain

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 ("Inscription", "crab"); 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, sensitive word :)"; 6 mesprocessor MP = new mesprocessor (); 7 MP. setmsg (MSG); 8 string result = MP. process (); 9 system. out. println (result); 10} 11}

Test results: [script], :), kdkjfejfid, crab :)

2. Improve the array and use arraylist to add the filterchain class.

 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 }

 

Modify 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 }

Test class

1 package COM. alvin; 2 3 Public class main {4 public static void main (string [] ARGs) {5 string MSG = "<SCRIPT>,:), kdkjfejfid, sensitive word :)"; 6 mesprocessor MP = new mesprocessor (); 7 MP. setmsg (MSG); 8 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}

Test results: [script], :), kdkjfejfid, crab :)

 

Design Pattern-responsibility chain pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.