From faec4b72ad1857ef7504177f8d23febc1df85326 Mon Sep 17 00:00:00 2001 From: Claudiu Farcas Date: Mon, 26 Apr 2021 07:57:50 +0300 Subject: [PATCH] CoR design pattern --- Program.cs | 6 +- .../ChainOfResponsabilitySample.cs | 99 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 behavioral/chainofresponsability/ChainOfResponsabilitySample.cs diff --git a/Program.cs b/Program.cs index 35d44e7..8b23897 100644 --- a/Program.cs +++ b/Program.cs @@ -14,6 +14,7 @@ using design_patterns.structural.decorator; using design_patterns.structural.facade; using design_patterns.structural.flyweight; using design_patterns.structural.proxy; +using design_patterns.behavioral.chainofresponsability; namespace design_patterns { @@ -39,7 +40,10 @@ namespace design_patterns // await DecoratorSample.Run(); // await FacadeSample.Run(); // await FlyweightSample.Run(); - await ProxySample.Run(); + // await ProxySample.Run(); + + // behavioral + await ChainOfResponsabilitySample.Run(); } catch (System.Exception ex) { diff --git a/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs b/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs new file mode 100644 index 0000000..0994369 --- /dev/null +++ b/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs @@ -0,0 +1,99 @@ +using System; +using System.Threading.Tasks; +using System.Collections.Generic + +namespace design_patterns.behavioral.chainofresponsability +{ + /// + /// Chain of Responsibility (CoR) is a behavioral design pattern that + /// lets you pass requests along a chain of handlers. + /// Upon receiving a request, each handler decides either + /// to process the request or to pass it to the next handler in the chain. + /// + /// Use it when: + /// - your program is expected to process different kinds of requests + /// in various ways, but the exact types of requests and their sequences + /// are unknown beforehand. + /// - it’s essential to execute several handlers in a particular order. + /// - the set of handlers and their order are supposed to change at runtime. + /// + public class ChainOfResponsabilitySample + { + public static async Task Run() + { + Console.WriteLine("Behavioral - ChainOfResponsability"); + + var request = new RequestInfo { + ID = Guid.NewGuid().ToString(), + Data = "request data", + Date = DateTime.Now + }; + + var handler = new BaseReqHandler { Request = request }; + handler.Add(new SecurityCheckReqHandler(false)); + handler.Add(new DataValidationReqHandler()); + + handler.Handle(); + + handler = new BaseReqHandler { Request = request }; + handler.Add(new SecurityCheckReqHandler(true)); + handler.Add(new DataValidationReqHandler()); + + handler.Handle(); + } + } + + public interface IReqHandler { + RequestInfo Request {get; set;} + void Add(IReqHandler reqHandler); + void Handle(); + } + + public class BaseReqHandler : IReqHandler { + public virtual RequestInfo Request { get; set; } + private IReqHandler next; + + public void Add(IReqHandler reqHandler){ + if(next!=null) { + next.Add(reqHandler); + } + else + { + reqHandler.Request = this.Request; + next = reqHandler; + } + } + public virtual void Handle() { + next?.Handle(); + } + } + + public class DataValidationReqHandler : BaseReqHandler { + public override void Handle() + { + Console.WriteLine($"DataValidating the request {Request.ID}"); + base.Handle(); + } + } + public class SecurityCheckReqHandler : BaseReqHandler { + private bool isSecure; + public SecurityCheckReqHandler(bool isSecure) => this.isSecure = isSecure; + + public override void Handle() + { + Console.WriteLine($"SecurityCheck the request {Request.ID}"); + if(isSecure) { + base.Handle(); + } + else { + Console.WriteLine("Unauthorized!"); + } + } + } + + public class RequestInfo { + public string ID { get; set; } + public string Data { get; set; } + public DateTime Date { get; set; } + } +} \ No newline at end of file