-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandInvoker.cs
More file actions
35 lines (29 loc) · 1002 Bytes
/
CommandInvoker.cs
File metadata and controls
35 lines (29 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace GofPatterns.Behavioral.CommandPattern;
/// <summary>
/// Command invoker class for command pattern.
/// It is responsible for holding the commands and executing the commands.
/// It can be directly used as a command invoker class, or inherited by other classes to make them command invoker.
/// </summary>
public class CommandInvoker<TCommand, TCommandRequest> : ICommandInvoker<TCommand, TCommandRequest>
where TCommand : ICommand<TCommandRequest> where TCommandRequest : ICommandRequest
{
private IList<TCommand> commands = new List<TCommand>();
public void AddCommand(TCommand command)
{
commands.Add(command);
}
public int ExecuteCommands()
{
var count = commands.Count;
if (count < 1)
return 0;
foreach (var command in commands)
command.Execute();
EmptyCommandList();
return count;
}
private void EmptyCommandList()
{
commands = new List<TCommand>();
}
}