A few years later, I will go back and review the implementation of the responsibility chain Design Model of Jack Ma.

Source: Internet
Author: User

After two years, I have moved from Java to C #. when reviewing the design model, I think of the implementation method of the responsibility chain of Jack Ma's teacher. This is too classic. What is classic is not the implementation of this chain, instead, the responsible chain class also implements the interface, so that the chain itself is also a filter, so that the instance of this class can be added to the instance of this class as a filter.

 

Using system; using system. collections. generic; public interface filter {string dofilter (string Str);} public class filterchain: Filter {list <filter> filters = new list <filter> (); public filterchain addfilter (filter f) {This. filters. add (f); return this;} Public String dofilter (string Str) {string r = STR; foreach (filter F in filters) {r = f. dofilter (r) ;}return R ;}} public class htmlfilter: Filter {Public String dofilter (string Str) {string r = Str. replace ('<','['). replace ('>', ']'); return r ;}} public class sensitivefilter: Filter {Public String dofilter (string Str) {string r = Str. replace ("employed", "employed "). replace ("sensitive", ""); return r ;}} public class facefilter: Filter {Public String dofilter (string Str) {return Str. replace (":)", "^ V ^") ;}} public class msgprocessor {public filterchain FC {Get; Set ;} Public String MSG {Get; set ;} public String process () {return FC. dofilter (this. MSG) ;}} public class program {static void main (string [] ARGs) {string MSG = "Hello everyone :), <SCRIPT>, sensitive, employed, I don't feel like teaching on the Internet, because I can't see the big guy "; msgprocessor MP = new msgprocessor (); MP. MSG = MSG; filterchain fc = new filterchain (). addfilter (New htmlfilter ()). addfilter (New sensitivefilter (); filterchain FC2 = new filterchain (). addfilter (New facefilter (); FC. addfilter (FC2); // classic, add one chain to another chain MP. fc = FC; string result = MP. process (); console. writeline (result); // The result console for filtering out invalid string characters. read ();}}

Related Article

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.