CSharp: Chain of Responsibility Pattern in donet core 6
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// 采购 /// </summary> public abstract class Approver { /// <summary> /// /// </summary> protected Approver? Next { get ; private set ; } /// <summary> /// /// </summary> /// <param name="next"></param> /// <returns></returns> public Approver RegisterNext(Approver next) { Next = next; return Next; } /// <summary> /// /// </summary> /// <param name="purchase"></param> public abstract void Approve(Purchase purchase); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// 审批 /// </summary> public class Purchase { /// <summary> /// 数量 /// </summary> public int Number { get ; set ; } /// <summary> /// 费用 ///</summary> public decimal Cost { get ; set ; } /// <summary> /// 审批 /// </summary> public string Purpose { get ; set ; } = string .Empty; } } using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Handlers { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// </summary> public class Director : Approver { /// <summary> /// /// </summary> /// <param name="purchase"></param> public override void Approve(Purchase purchase) { if (purchase.Cost < 1000) { Console.WriteLine($ "董事批准了这笔购买 #{purchase.Number} 这成本 {purchase.Cost:C}" ); return ; } Next?.Approve(purchase); } } } using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Handlers { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// </summary> public class ExecutiveMeeting : Approver { /// <summary> /// /// </summary> public static readonly ExecutiveMeeting Instance = new (); /// <summary> /// /// </summary> private ExecutiveMeeting() { } /// <summary> /// /// </summary> /// <param name="purchase"></param> public override void Approve(Purchase purchase) => Console.WriteLine( $ "这次采购#{purchase.Number} 成本是 {purchase.Cost:C} " + "需要召开一次行政会议才能得到批准." ); } } using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Handlers { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// </summary> public class Manager : Approver { /// <summary> /// /// </summary> /// <param name="purchase"></param> public override void Approve(Purchase purchase) { if (purchase.Cost < 500) { Console.WriteLine($ "经理批准了这笔购买 #{purchase.Number} 成本是 {purchase.Cost:C}" ); return ; } Next?.Approve(purchase); } } } using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Handlers { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// </summary> public class President : Approver { /// <summary> /// /// </summary> /// <param name="purchase"></param> public override void Approve(Purchase purchase) { if (purchase.Cost < 5000) { Console.WriteLine($ "董事长批准了购买 #{purchase.Number} 成本是 {purchase.Cost:C}" ); return ; } Next?.Approve(purchase); } } } using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Handlers { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// </summary> public class VicePresident : Approver { /// <summary> /// /// </summary> /// <param name="purchase"></param> public override void Approve(Purchase purchase) { if (purchase.Cost < 2000) { Console.WriteLine($ "副董事长批准了这次购买 #{purchase.Number} 成本是 {purchase.Cost:C}" ); return ; } Next?.Approve(purchase); } } } using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Common; using Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.Handlers; using Geovin.Du.BuildingBlocks; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval { /// <summary> /// 责任链模式Chain of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility /// </summary> public static class PurchaseApprovalExecutor { /// <summary> /// /// </summary> public static void Execute() { ConsoleExtension.WriteSeparator( "责任链模式Chain of Responsibility Pattern--Purchase approval demo" ); Approver manager = new Manager(); Approver director = new Director(); Approver vicePresident = new VicePresident(); Approver president = new President(); manager .RegisterNext(director) .RegisterNext(vicePresident) .RegisterNext(president) .RegisterNext(ExecutiveMeeting.Instance); var lowCostPurchase = new Purchase { Number = 1, Cost = 400, Purpose = "白板上购买." }; var mediumCostPurchase = new Purchase { Number = 2, Cost = 3000, Purpose = "笔记本电脑购买e." }; var highCostPurchase = new Purchase { Number = 3, Cost = 11500, Purpose = "汽车的购买." }; manager.Approve(lowCostPurchase); manager.Approve(mediumCostPurchase); manager.Approve(highCostPurchase); } } } |
调用:
1 | Geovin.Du.DuChainOfResponsibility.DuPurchaseApproval.PurchaseApprovalExecutor.Execute(); |
输出:、
1 2 3 4 5 6 7 | Hello, Geovin Du! 学习 .net 6 责任链模式Chain of Responsibility Pattern--Purchase approval demo -------------------------------------------------- 经理批准了这笔购买 #1 成本是 ¥400.00 董事长批准了购买 #2 成本是 ¥3,000.00 这次采购#3 成本是 ¥11,500.00 需要召开一次行政会议才能得到批准. |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
desgin patterns
, 设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2021-12-17 java: MySql Connection using JDK 14.02
2021-12-17 java: Lamdba
2015-12-17 css:Media Queries