Command Design Pattern in C#

The Command Pattern falls under the category of Behavioural Design Patterns. If you have quite amount of experience in C# particularly WPF, you must have used DelegateCommand or Routed Command or RelayCommands. This internally uses the Command Pattern only. The Command Pattern can be used in any of the projects and we will quicky understand what is it and how to use it in our project.

The Command Pattern encapsulates a request as an object and gives it a known public interface. A request or action is mapped and stored as an object. The Invoker will be ultimately responsible for processing the request. This clearly decouples the request from the invoker. This is more suited for scenarios where we implement Redo, Copy, Paste and Undo operations where the action is stored as an object. We generally use Menu or Shortcut key gestures for any of the preceding actions to be executed.

Let us run through some sample code for the Command Pattern.

  1. Define a command for processing an action or request. For demo purposes, I am defining two commands, StartCommand and StopCommand.

    public interface ICommand
    {
       
    string Name { get; }
       
    void Execute();

    public class StartCommand : ICommand
    {
       
    public void Execute()
        {
           
    Console.WriteLine("I am executing StartCommand");
        } 
       
    public string Name
        {
           
    get { return "Start"; }
        }

    public class StopCommand : ICommand
    {
       
    public void Execute()
        {
           
    Console.WriteLine("I am executing StopCommand");
        } 
       
    public string Name
        {
           
    get { return "Stop"; }
        }
    }
     
  2. Now that we have defined the commands, we need to define Invoker that will be responsible for executing the commands. This is more of a factory pattern that will execute the requested command.

    public class Invoker
    {
       
    ICommand cmd = null;
       
    public ICommand GetCommand(string action)
        {
           
    switch (action)
            {
               
    case "Start":
                    cmd =
    new StartCommand();
                   
    break;
               
    case "Stop":
                    cmd =
    new StopCommand();
                   
    break;
               
    default:
                   
    break;
            }
           
    return cmd;
        }
    }
     
  3. Then from my application, I will just add logic to just invoke a specific command.

    class Program
    {
       
    static void Main(string[] args)
        {
           
    Invoker invoker = new Invoker();
           
    // Execute Start Command
            ICommand command = invoker.GetCommand("Start");
            command.Execute();
           
    // Execute Stop Commad
            command = invoker.GetCommand("Stop");
            command.Execute();
           
    Console.WriteLine("Command Pattern demo");
           
    Console.Read();
        }
    }

Command Pattern is a useful and very widely used pattern. This is just one of the approaches for demonstrating the usage of a Command Pattern. Again, we can customize the code as needed.

I am also attaching the sample of this code for the demo. Please feel free to comment with feedback / clarifications if you have any.

Happy Coding! 

Up Next
    Ebook Download
    View all
    Learn
    View all