Advertisement
jwow22

Command Handler

Jan 5th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. public class CommandHandler
  4. {
  5.     public Stack<Command> CommandStack;
  6.  
  7.     public CommandHandler()
  8.     {
  9.         CommandStack = new Stack<Command>();
  10.     }
  11.  
  12.     public void ExecuteCommand(Command command)
  13.     {
  14.         command.CommandHandler = this;
  15.         command.Execute();
  16.     }
  17.    
  18.     public void Undo()
  19.     {
  20.         if (EmptyStack()) { return; }
  21.         LastCommand().Undo();
  22.         RemoveLastCommand();
  23.     }
  24.    
  25.     public bool EmptyStack() { return CommandStack.Count == 0; }
  26.  
  27.     public Command LastCommand()
  28.     {
  29.         return CommandStack.Count == 0 ? null : CommandStack.Peek();
  30.     }
  31.    
  32.     public Command RemoveLastCommand() { return CommandStack.Pop(); }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement