CSharp: Chain of Responsibility Pattern in donet core 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | /// <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); } } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //责任链模式 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(); |
输出:
1 2 3 4 5 6 7 8 | ***责任链模式 Chain of Responsibility Pattern Demo*** 已处理传真错误处理程序 正常 优先排队 : fax. 处理电子邮件错误处理程序高 优先排队: email邮件没有到达目的地. 处理电子邮件错误处理程序正常 优先排队: email在电子邮件中,抄送字段总是禁用的. 已处理传真错误处理程序 高 优先排队 : fax传真没有到达目的地 未知错误发生。立即咨询专家 已处理传真错误处理程序 高 优先排队 : fax传真和电子邮件都不能用. |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2019-10-12 css: hide or dispaly div
2019-10-12 Python: simple code