From e0bac4a96c538d9cf13e12e6d1df2cff35475b4b Mon Sep 17 00:00:00 2001 From: Claudiu Farcas Date: Mon, 26 Apr 2021 08:25:59 +0300 Subject: [PATCH] command design pattern --- Program.cs | 4 +- .../ChainOfResponsabilitySample.cs | 10 ++- behavioral/command/CommandSample.cs | 65 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 behavioral/command/CommandSample.cs diff --git a/Program.cs b/Program.cs index 8b23897..2c9b28a 100644 --- a/Program.cs +++ b/Program.cs @@ -15,6 +15,7 @@ using design_patterns.structural.facade; using design_patterns.structural.flyweight; using design_patterns.structural.proxy; using design_patterns.behavioral.chainofresponsability; +using design_patterns.behavioral.command; namespace design_patterns { @@ -43,7 +44,8 @@ namespace design_patterns // await ProxySample.Run(); // behavioral - await ChainOfResponsabilitySample.Run(); + // await ChainOfResponsabilitySample.Run(); + await CommandSample.Run(); } catch (System.Exception ex) { diff --git a/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs b/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs index 0994369..e5e1c07 100644 --- a/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs +++ b/behavioral/chainofresponsability/ChainOfResponsabilitySample.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using System.Collections.Generic namespace design_patterns.behavioral.chainofresponsability { @@ -29,16 +28,18 @@ namespace design_patterns.behavioral.chainofresponsability Date = DateTime.Now }; + //define chain of handlers var handler = new BaseReqHandler { Request = request }; handler.Add(new SecurityCheckReqHandler(false)); handler.Add(new DataValidationReqHandler()); - + // issue the handler handler.Handle(); + //define chain of handlers handler = new BaseReqHandler { Request = request }; handler.Add(new SecurityCheckReqHandler(true)); handler.Add(new DataValidationReqHandler()); - + // issue the handler handler.Handle(); } } @@ -47,6 +48,9 @@ namespace design_patterns.behavioral.chainofresponsability RequestInfo Request {get; set;} void Add(IReqHandler reqHandler); void Handle(); + // alternatively we can invoke Handle with the main request object + // instead storing it as property + // void Handle(RequestInfo request); } public class BaseReqHandler : IReqHandler { diff --git a/behavioral/command/CommandSample.cs b/behavioral/command/CommandSample.cs new file mode 100644 index 0000000..e270ea6 --- /dev/null +++ b/behavioral/command/CommandSample.cs @@ -0,0 +1,65 @@ +using System; +using System.Threading.Tasks; + +namespace design_patterns.behavioral.command +{ + /// + /// Command is a behavioral design pattern that turns a request + /// into a stand-alone object that contains all information about the request. + /// This transformation lets you pass requests + /// as a method arguments, delay or queue a request’s execution, + /// and support undoable operations. + /// + /// Use when: + /// - you want to parametrize objects with operations. + /// - you want to queue operations, schedule their execution, + /// or execute them remotely. (serialize commands) + /// - you want to implement reversible operations. + /// + public class CommandSample + { + public static async Task Run() + { + Console.WriteLine("Behavioral - Command"); + + var order = new Order { + ID = Guid.NewGuid().ToString() + }; + ICommand addCommand = new AddOrderCommand(order); + addCommand.Execute(); + ICommand removeCommand = new RemoveOrderCommand(order.ID); + removeCommand.Execute(); + } + } + + public interface ICommand + { + void Execute(); + } + + public class AddOrderCommand : ICommand { + public AddOrderCommand(Order order) => this.Order = order; + + public Order Order { get; private set; } + + public void Execute() + { + Console.WriteLine($"Execute add order command for order {Order.ID}"); + } + } + + public class RemoveOrderCommand : ICommand { + public RemoveOrderCommand(string orderId) => this.OrderID = orderId; + + public string OrderID { get; private set; } + + public void Execute() + { + Console.WriteLine($"Execute remove order command for order {OrderID}"); + } + } + + public class Order { + public string ID { get; set; } + } +} \ No newline at end of file