command design pattern

This commit is contained in:
Claudiu Farcas
2021-04-26 08:25:59 +03:00
parent faec4b72ad
commit e0bac4a96c
3 changed files with 75 additions and 4 deletions
+3 -1
View File
@@ -15,6 +15,7 @@ using design_patterns.structural.facade;
using design_patterns.structural.flyweight; using design_patterns.structural.flyweight;
using design_patterns.structural.proxy; using design_patterns.structural.proxy;
using design_patterns.behavioral.chainofresponsability; using design_patterns.behavioral.chainofresponsability;
using design_patterns.behavioral.command;
namespace design_patterns namespace design_patterns
{ {
@@ -43,7 +44,8 @@ namespace design_patterns
// await ProxySample.Run(); // await ProxySample.Run();
// behavioral // behavioral
await ChainOfResponsabilitySample.Run(); // await ChainOfResponsabilitySample.Run();
await CommandSample.Run();
} }
catch (System.Exception ex) catch (System.Exception ex)
{ {
@@ -1,6 +1,5 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic
namespace design_patterns.behavioral.chainofresponsability namespace design_patterns.behavioral.chainofresponsability
{ {
@@ -29,16 +28,18 @@ namespace design_patterns.behavioral.chainofresponsability
Date = DateTime.Now Date = DateTime.Now
}; };
//define chain of handlers
var handler = new BaseReqHandler { Request = request }; var handler = new BaseReqHandler { Request = request };
handler.Add(new SecurityCheckReqHandler(false)); handler.Add(new SecurityCheckReqHandler(false));
handler.Add(new DataValidationReqHandler()); handler.Add(new DataValidationReqHandler());
// issue the handler
handler.Handle(); handler.Handle();
//define chain of handlers
handler = new BaseReqHandler { Request = request }; handler = new BaseReqHandler { Request = request };
handler.Add(new SecurityCheckReqHandler(true)); handler.Add(new SecurityCheckReqHandler(true));
handler.Add(new DataValidationReqHandler()); handler.Add(new DataValidationReqHandler());
// issue the handler
handler.Handle(); handler.Handle();
} }
} }
@@ -47,6 +48,9 @@ namespace design_patterns.behavioral.chainofresponsability
RequestInfo Request {get; set;} RequestInfo Request {get; set;}
void Add(IReqHandler reqHandler); void Add(IReqHandler reqHandler);
void Handle(); 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 { public class BaseReqHandler : IReqHandler {
+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Threading.Tasks;
namespace design_patterns.behavioral.command
{
/// <summary>
/// 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 requests 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.
/// </summary>
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; }
}
}