undo for command dp

This commit is contained in:
Claudiu Farcas
2021-04-26 08:36:28 +03:00
parent e0bac4a96c
commit 9b547e4e1c
+37 -4
View File
@@ -1,5 +1,7 @@
using System.Windows.Input;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic;
namespace design_patterns.behavioral.command namespace design_patterns.behavioral.command
{ {
@@ -25,26 +27,47 @@ namespace design_patterns.behavioral.command
var order = new Order { var order = new Order {
ID = Guid.NewGuid().ToString() ID = Guid.NewGuid().ToString()
}; };
ICommand addCommand = new AddOrderCommand(order); List<ICommand> operations = new List<ICommand>();
addCommand.Execute(); operations.Add(new AddOrderCommand(order));
ICommand removeCommand = new RemoveOrderCommand(order.ID); operations.Add(new RemoveOrderCommand(order.ID));
removeCommand.Execute();
// batch execution
foreach(var cmd in operations){
cmd.Execute();
}
// undo
operations.Reverse();
foreach(var cmd in operations){
cmd.Undo();
}
} }
} }
public interface ICommand public interface ICommand
{ {
bool HasSucceded {get;set;}
void Execute(); void Execute();
void Undo();
} }
public class AddOrderCommand : ICommand { public class AddOrderCommand : ICommand {
public AddOrderCommand(Order order) => this.Order = order; public AddOrderCommand(Order order) => this.Order = order;
public Order Order { get; private set; } public Order Order { get; private set; }
public bool HasSucceded { get; set; }
public void Execute() public void Execute()
{ {
Console.WriteLine($"Execute add order command for order {Order.ID}"); Console.WriteLine($"Execute add order command for order {Order.ID}");
HasSucceded = true;
}
public void Undo()
{
if(HasSucceded){
Console.WriteLine($"UNDO - add order for order {Order.ID}");
}
} }
} }
@@ -52,10 +75,20 @@ namespace design_patterns.behavioral.command
public RemoveOrderCommand(string orderId) => this.OrderID = orderId; public RemoveOrderCommand(string orderId) => this.OrderID = orderId;
public string OrderID { get; private set; } public string OrderID { get; private set; }
public bool HasSucceded { get; set; }
public void Execute() public void Execute()
{ {
Console.WriteLine($"Execute remove order command for order {OrderID}"); Console.WriteLine($"Execute remove order command for order {OrderID}");
// FORCE failure
HasSucceded = false;
}
public void Undo()
{
if(HasSucceded){
Console.WriteLine($"UNDO - remove order for order {OrderID}");
}
} }
} }