10 January 2010 ~ 1 Comment

Command design pattern

Hi!

In this post, I will show you how the Command design pattern can help us and serve as a powerful tool for software development, web and desktop alike.

With the help of the Command design pattern, we can bring object oriented programming into a command mechanism, which is otherwise not so object oriented.
Usually, it is acceptable to create classes and methods that operate inside those classes with received paramaters.
Some of these functions actually work as command executes, and they can receive anything from an object to a primitive type such as an integer or string.

By wrapping a basic command inside a Command design pattern, we can add extra processing to the command object, gather information from it and to it, add paramaters and a lot more.

Lets begin:

The next method shows us a regular way in receiving a command:

public void Execute(int command)
{
if (command == 0)
{
// do something
}
else if (command == 1)
{
// do something
}
}

This method will do the job, but its very hard to maintain in the case of expansion, and it cannot receive additional functionality.
By using the Command design pattern, we can turn the command execution into a generic operation, with a much larger functionality and much more freedom of space for future extensibility.

And this is how its done:
We are to create an abstract class named Command, which will serve as the base class for all type of commands that we would want to create.
Inside the Command class, we will add a variable of type IReceiver, which will be an interface in charge of the command implementation.

So, we will have the following class:

public abstract class Command
{
     private IReceiver receiver;

     public Command(IReceiver receiver)
     {
          this.receiver = receiver;
     }

     public abstract void Execute();
}

Notice the abstract method Execute, which will be used to carry out the interface implementation.
This gives us larger freedom in what we want to accomplish, and each Command driven class will be able to add extra functionality suiting its own needs.

The interface will be declared like this:

public interface IReceiver
{
     void DoAction(string strCommand);
}

Now, we will create a class that implements IReceiver for the execution of a certain command.

public class LessonPrinter : IReceiver
{
     void IReceiver.DoAction(string strCommand)
     {
          Console.WriteLine(strCommand);
     }
}

All we have left to do now is create a Command class, and create the object who will receive a command and execute it.
Lets create a new command based on our abstract Command class, a new class named MyCommand:

public class MyCommand : Command
{
     protected string strCommand;

     public MyCommand(IReceiver receiver, string strCommand)
             : base(receiver)
     {
          this.strCommand = strCommand;
     }

     public override void Execute()
     {
          this.receiver.DoAction(strCommand);
     }
}

As you can see, this class receives an IReceiver object for executing a command, and a paramater that will be passed into the IReceiver object.
The Execute function is caling the IReceiver objects’ DoAction methods.

Now, we will create an object who will receieve and run commands, and also supply us with information about the list of commands inserted.

public class Invoker
{
     private List<Command> m_log = new List<Command>();

     public void Execute(Command command)
     {
          this.m_log.Add(command);
          command.Execute();
     }

     public List<Command> GetLog()
     {
          return this.m_log;
     }
}

Finally, we can put it all together:

LessonPrinter = new LessonPrinter();
Invoker Invoker = new Invoker();

Command _command  = new MyCommand(LessonPrinter,"hello yaron!");
Invoker.Execute(_command);

List<Command> commands = invoker.GetLog();

And.. thats it!
For questions and anything else – yaron@codeoasis.com

Bookmark and Share