Command Design Pattern

Posted: April 14, 2013 in Uncategorized

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 Command Pattern only. 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. Request or action is mapped and stored as an object. Invoker will be ultimately responsible for processing the request. This clearly decouples request from the invoker. This is more suited for scenarios where we implement Redo, Copy, Paste and Undo operations where action is stored as an object. We generally use Menu or Shortcut key gestures for any of the above actions to be executed.

Let us run through sample code for the Command Pattern.

  1. Define a Command for processing action or request. For demo purpose, 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 Commands, we need to define Invoker which will be responsible for executing the Commands. This is more of a Factory pattern which will execute 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 specific commands.

 

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 one of useful and most widely used patterns. This is just one of the approaches for demonstrating usage of Command Pattern. Again, we can customize code as per our needs.

 

I am also attaching sample of this  Code which is used for demo. Please feel free to comment feedback / clarifications if you have any.

 

Happy Coding!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s