CSharp: Chain of Responsibility Pattern in donet core 3

 

 /// <summary>
    ///责任链模式 Chain of Responsibility Pattern  亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility
    /// geovindu,Geovin Du edit
    /// </summary>
    public enum MessagePriority
    {
        /// <summary>
        /// 正常
        /// </summary>
        [Description("正常")]
        Normal = 1,
        /// <summary>
        /// 高
        /// </summary>
        [Description("高")]
        High =2
    }


    public static class geovindu
    {

        /// <summary>
        /// 扩展方法,获得枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
        /// <returns>枚举的Description</returns>
        public static string GetDescription(this Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }

            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute?.Description;
        }
    }


    /// <summary>
    /// Message class
    /// </summary>
    public class Message
    {
        public string Text { get; set; }
        public MessagePriority Priority;
        /// <summary>
        /// 
        /// </summary>
        public string Ms { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="priority"></param>
        public Message(string msg, MessagePriority priority)
        {
            this.Text = msg;
            this.Priority = priority;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ms"></param>
        public Message(string msg, string ms)
        {
            this.Text = msg;
            this.Ms = ms;
        }
    }
    /// <summary>
    /// Abstract class -Receiver
    /// The abstract class is chosen to share 
    /// the common codes across derived classes.
    /// </summary>
    abstract class Receiver
    {
        protected Receiver nextReceiver;
        //To set the next handler in the chain.
        public void NextReceiver(Receiver nextReceiver)
        {
            this.nextReceiver = nextReceiver;
        }
        public abstract void HandleMessage(Message message);
    }
    /// <summary>
    /// Fax Error Handler class
    /// </summary>
    class FaxErrorHandler : Receiver
    {
        bool messagePassedToNextHandler = false;
        public override void HandleMessage(Message message)
        {
            //Start processing if the error message contains "fax"
            if (message.Text.Contains("fax"))
            {
                Console.WriteLine($" 已处理传真错误处理程序 {message.Ms}  优先排队 : {message.Text}");
                //Do not leave now, if the error message contains email too.
                if (nextReceiver != null && message.Text.Contains("email"))
                {
                    Console.WriteLine("我已经修复了传真侧的缺陷。现在电子邮件团队需要在这个修复的顶部工作");
                    nextReceiver.HandleMessage(message);
                    //We'll not pass the message repeatedly to next handler.
                    messagePassedToNextHandler = true;
                }
            }
            if (nextReceiver != null && messagePassedToNextHandler != true)
            {
                nextReceiver.HandleMessage(message);
            }
        }
    }
    /// <summary>
    /// Email Error Handler class
    /// </summary>
    class EmailErrorHandler : Receiver
    {
        bool messagePassedToNextHandler = false;
        public override void HandleMessage(Message message)
        {
            //Start processing if the error message contains "email"
            if (message.Text.Contains("email"))
            {
                Console.WriteLine($"处理电子邮件错误处理程序{message.Ms}  优先排队: {message.Text}");
                //Do not leave now, if the error message contains "fax" too.
                if (nextReceiver != null && message.Text.Contains("fax"))
                {
                    Console.WriteLine("电子邮件侧缺陷修复。现在传真组需要交叉验证这个修复");
                    //Keeping the following code here.
                    //It can be useful if you place this handler before fax error handler
                    nextReceiver.HandleMessage(message);
                    //We'll not pass the message repeatedly to the next handler.
                    messagePassedToNextHandler = true;
                }
            }
            if (nextReceiver != null && messagePassedToNextHandler != true)
            {
                nextReceiver.HandleMessage(message);
            }
        }
    }
    /// <summary>
    /// UnknownErrorHandler class
    /// </summary>
    class UnknownErrorHandler : Receiver
    {
        public override void HandleMessage(Message message)
        {
            if (!(message.Text.Contains("fax") || message.Text.Contains("email")))
            {
                Console.WriteLine("未知错误发生。立即咨询专家");
            }
            else if (nextReceiver != null)
            {
                nextReceiver.HandleMessage(message);
            }
        }
    }

  

调用:

 //责任链模式 
            Console.WriteLine("***责任链模式 Chain of Responsibility Pattern Demo***\n");

            //Different handlers
            Receiver emailHandler = new EmailErrorHandler();
            Receiver faxHandler = new FaxErrorHandler();
            Receiver unknownHandler = new UnknownErrorHandler();

            faxHandler.NextReceiver(emailHandler);
            emailHandler.NextReceiver(unknownHandler);
            string du=geovindu.GetDescription(MessagePriority.Normal, true);
            string geovin=geovindu.GetDescription(MessagePriority.High, true);
            Message msg = new Message("fax.", du); //MessagePriority.Normal
            faxHandler.HandleMessage(msg);
            msg = new Message("email邮件没有到达目的地.", geovin);
            faxHandler.HandleMessage(msg);
            msg = new Message("email在电子邮件中,抄送字段总是禁用的.", du);
            faxHandler.HandleMessage(msg);
            msg = new Message("fax传真没有到达目的地", geovin);
            faxHandler.HandleMessage(msg);
            msg = new Message("无法登录系统。.", geovin);
            faxHandler.HandleMessage(msg);
            msg = new Message("fax传真和电子邮件都不能用.", geovin);
            faxHandler.HandleMessage(msg);
            Console.ReadKey();

  

输出:

***责任链模式 Chain of Responsibility Pattern Demo***

 已处理传真错误处理程序 正常  优先排队 : fax.
处理电子邮件错误处理程序高  优先排队: email邮件没有到达目的地.
处理电子邮件错误处理程序正常  优先排队: email在电子邮件中,抄送字段总是禁用的.
 已处理传真错误处理程序 高  优先排队 : fax传真没有到达目的地
未知错误发生。立即咨询专家
 已处理传真错误处理程序 高  优先排队 : fax传真和电子邮件都不能用.

  

 

posted @ 2022-10-12 06:24  ®Geovin Du Dream Park™  阅读(26)  评论(0)    收藏  举报