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 ();}}