Is it possible implement the GOF command pattern using a Queue of Action delegates?
I have been trying to wrap my head around it for a while and I am stumped because each of the possible actions I want to add to the queue may have a varing number of parameters.
Any suggestions? Am I barking up the wrong tree by focusing on the command pattern?
UPDATE:
Many thanks jgauffin, it works a treat... my implementation now looks like
public class CommandDispatcher
{
private readonly Dictionary<Type, List<Action<ICommand>>> _registeredCommands =
new Dictionary<Type, List<Action<ICommand>>>();
public void RegisterCommand<T>(Action<ICommand> action) where T : ICommand
{
if (_registeredCommands.ContainsKey(typeof (T)))
_registeredCommands[typeof (T)].Add(action);
else
_registeredCommands.Add(typeof (T), new List<Action<ICommand>> {action});
}
public void Trigger<T>(T command) where T : ICommand
{
if (!_registeredCommands.ContainsKey(typeof(T)))
throw new InvalidOperationException("There are no subscribers for that command");
foreach (var registeredCommand in _registeredCommands[typeof(T)])
{
registeredCommand(command);
if (command.Cancel) break;
}
}
}