Implementing the Command Pattern using C# Action delegate
Asked Answered
D

4

7

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;
        }
    }
}
Decameter answered 11/10, 2010 at 5:44 Comment(0)
A
10

You can use an Action. You should not use multiple parameters. What happens if a command needs a new parameter? Then you would need to change all places invoking the command plus the handler.

Instead, you should use Command classes which has all parameters as properties. In this way you can add parameters without it affecting the code (the new parameters should be treated as optional in the handler).

this is how I would do it:

public interface ICommand
{
    // Cancel processing, do not invoke any more handlers
    public bool Cancel { get; set; }
}

public class CommandDispatcher 
{
  private Dictionary<Type, List<Action<ICommand>>> _commands = new Dictionary<Type, List<Action<ICommand>>>();


  // Add to dictionary here
  public void Subscribe<T>(Action<T> action) where T : ICommand
  {
      List<Action<ICommand>> subscribers;
      if (!_commands.TryGetValue(typeof(T), out subscribers))
      {
          subscribers = new List<Action<ICommand>>();
          _commands.Add(typeof(T), subscribers));
      }

      subscribers.Add(action);
  }

  // find command and to foreach to execute the actions      
  public void Trigger<T>(T command) where T : ICommand
  {
      List<Action<ICommand>> subscribers;
      if (!_commands.TryGetValue(typeof(T), out subscribers))
          throw new InvalidOperationException("There are no subscribers for that command");

      foreach(var subsriber in subscribers)
      {
          subscriber(command);
          if (command.Cancel)
              break; //a handler canceled the command to prevent others from processing it.
      }
  }

}

public class AddTextCommand : ICommand
{
    public string TextToAdd {get;set;}
}

public class TextHandler
{
    public TextHandler(CommandDispatcher dispatcher)
    {
        disptacher.Subscribe<AddTextCommand>(OnAddText);
    }

    public void OnAddText(AddTextCommand cmd)
    {
        //....
    }
}


public partial class MyForm : Form
{
    CommandDispatcher _dispatcher;

    private void MyTextBox_Changed(object source, EventArgs e)
    {
        _dispatcher.Trigger(new AddTextCommand{TextToAdd = MyTextBox.Text}=;
    } 
}

Note that the code is kind of pseudo-code. I've written it directly in the answer without testing it. You will probably have to change stuff in order to get it working, but it should at least give you a hint. The implementation let's you add multiple subscribers for each command.

Appease answered 11/10, 2010 at 6:15 Comment(4)
This seems like an interesting solution, would you mind elaborating how the trigger method would work? command(T) or command.Invoke()? And why a dictionary of _commands, why not just a straight List or Queue?Decameter
Dictionary is faster, you don't have to traverse the collection by yourself to find the correct command type (and it's subscribers)Appease
I've added implementations. They may not work 100%. Try to get it working.Appease
If you don't want to implement it by yourself you can use my Griffin.Decoupled: blog.gauffin.org/2012/10/introducing-griffin-decoupledAppease
S
3

In command pattern, typical command interface would have simple execute method - this can be represented by Action delegate. But actual implementation will be provided by different concrete classes where you will/can pass parameters (for example, via constructor). For example:

public interface ICommand 
{
   public void Execute();
}

public class Command1 : ICommand
{
   public Command1(int param1, string param2)
   {
   }

   ...
}

public class Command2 : ICommand
{
  ...
}

public class Program
{

   public static void Main()
   {

       ...

       var commands = new List<Action>();
       commands.Add((new Command1(3, "Hello")).Execute);
       commands.Add((new Command2(...)).Execute);

       ...
   }


}

The point here is that command related state and implementation would be encapsulated within different implementation while Action delegate will point to its instance method. So invoking the delegate will result in execution of the command.

Snake answered 11/10, 2010 at 6:23 Comment(0)
U
1

If you're concerned with the number of parameters, then properly implementing the command pattern using a class would be the right way to go. The Action delegate is limited to only one. Also, if you use the Action delegate, you might want to implement an Undo later on which you won't be able to do since you just used a delegate instead of a class.

Untruthful answered 11/10, 2010 at 5:56 Comment(0)
V
0

Another "minimalistic" way would be to utilize the properties of C# closures to capture the required parameters automatically. Minimalistic because we don't even have an ICommand interface here (since we use Action directly) let alone any concrete implementations. Might be useful in some scenarios.

See the below LinqPad example:

    void Main()
    {
        var commands = new List<Action>();
    
        commands.Add(MakeCommandOne("CommandOne"));
        commands.Add(MakeCommandTwo("CommandTwo", 5));
    
        var p3_1 = "parameter3_1";
        var p3_2 = 32;
        var name3 = "CommandThree";
        commands.Add(
        () => Console.WriteLine($"Name: {name3}, p1: {p3_1}, p2: {p3_2}")
        );
        
        RunAllCommands(commands);
    }
    
    void RunAllCommands(List<Action> commands)
    {
        foreach (var command in commands)
        {
            command();
        }
    }
    
    Action MakeCommandOne(string name) => () => Console.WriteLine($"Name: {name}");
    Action MakeCommandTwo(string name, double age) => () => Console.WriteLine($"Name: {name}, age: {age}");
Varicotomy answered 22/7, 2022 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.