职责链模式 通过管理端进行解耦

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication20
{
    public interface IFiler
    {
        string doFilter(string str);
    }

    public class AFilter:IFiler
    {
        public string doFilter(string str)
        {
            return str.Replace("你妈的", "向你妈妈问好");
        }
    }

    public class BFilter : IFiler
    {
        public string doFilter(string str)
        {
           return  str.Replace("你舅舅的", "向你舅舅问好");
        }
    }

    public class CFilter : IFiler
    {
        public string doFilter(string str)
        {
            return str.Replace("你大爷的", "向你大爷问好");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //客户端处理敏感信息
            string msg = "我们都是好孩子 你妈的 你大爷的 你舅舅的";
           
            FilterChain fc=new FilterChain();
            fc.AddFilter(new AFilter()).AddFilter(new BFilter()).AddFilter(new CFilter());
            Console.WriteLine("------处理之前------");
            Console.WriteLine(msg);
            msg=fc.SetFilter(msg);
            Console.WriteLine("------处理之后------");
            Console.WriteLine(msg);
            Console.ReadKey();


        }
    }

    public class FilterChain
    {
        IList<IFiler>  fcFilers=new List<IFiler>();

        public FilterChain AddFilter(IFiler f)
        {
            fcFilers.Add(f);
            return this;
        }

        public string SetFilter(string msg)
        {
            foreach (var fc in fcFilers)
            {
                msg=fc.doFilter(msg);
            }

            return msg;
        }

    }
}

原文地址:https://www.cnblogs.com/kexb/p/4507671.html