Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3223582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:07:26+00:00 2026-05-17T16:07:26+00:00

Is it possible implement the GOF command pattern using a Queue of Action delegates?

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:07:26+00:00Added an answer on May 17, 2026 at 4:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to implement a non-blocking client socket? I have tried using sockfd
Possible Duplicate: implement actionsheet in android I am trying to implement the action sheet
Is it possible to implement some kind of timeout (time limit) for fork using
Is it possible to implement navigation using jquery ui tabs instead of standard tabs
Is it possible to implement Delegate methods for FileSystemWatcher? I have a delegate method
Is it possible to implement associative arrays as Selectors and Values. I have an
Is it possible to implement a forgot password using ASP.Net membership provider? Any sample
Is it possible to implement the model–view–controller pattern in Java for Android? Or is
Is it possible to implement Remember Me using jquery? If so, any suggestion how
Is it possible to implement a p2p using just PHP? Without Flash or Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.