19 October 2013

Command Design Pattern

Command Design Pattern is helpful in some situations. People often talk about this design pattern without having the full understanding of the pattern. If it comes to j2ee frameworks, one of the most popular framework “Struts” uses this design pattern in an efficient manner. So what is Command Design pattern? and why is it named as Command Design Pattern?

Understanding Command Design Pattern and Why Named So:-
In computers command is not a new term. As humans treat themselves as masters and computers as slaves(as computers cannot think by themselves) the word command is suited for executing a set of instruction when some small word is passed to the computer called as command. 

For example in windows OS if you pass a command “dir” in the command prompt it returns the list of files in the current directory.  Here in this case the “dir” in windows is reserved to perform something. In depth the word “dir” is reserved and mapped to “dir.exe” which does the execution part. The dir.exe passes the set of instructions to the computer which is nothing but the receiver of the instruction that are executed when command is issued. The computer (receiver) then responds to the instructions passed and returns the list of files in the current directory. 

so now we understood the command implementor (dir.exe) which is nothing but can be called as command and the receiver (nothing but the computer in this case)   which receives the set or a single instructions from the command execution class. There is still one more important component that we missed which is nothing but invoker. When command is issued the invoker selects the “command implemented class or exe” which is required to execute the said command and executes the appropriate command class or executable. 

So finally there are three important components involved in Command Design Pattern. They are “Invoker” , “Command (command Implementor)” and the “Receiver”

Invoker selects the appropriate command implementor and executes the command. The receiver receives the instructions and will act accordingly. Finally the command issuer gets the desired output.

Now The Most Important Thing Is, What Problem Does It solve?
suppose when some common operations have to be performed around the command execution then this pattern is the answer for such cases. In this pattern the “Invoker” invokes the command. So if the cleanup and initialization operations have to be performed around the command execution the “Invoker” does that. This is the answer for such situations.

Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the class of the method or the method parameters. Using an invoker object allows bookkeeping about command executions to be conveniently performed, as well as implementing different modes for commands, which are managed by the invoker object, without the need for the client to be aware of the existence of bookkeeping or modes.

The following is the example in java for Command Pattern:-

ICommand.java
public interface ICommand{
    public void execute();
}

DirCommand.java
public class DirCommand implements ICommand{
        public void execute(){
               //code for listing the files in the current directory on console.
        }
}

Invoker.java
public class Invoker{

      public void intialize(){
        // perform some comman intialization operations
      }

      public void invoke(String command){
             intialize();
             String finalCommandClassName=command+”Command”;
             // code for instantiating the “finalCommandClass”.
            finalCommandClass.execute();
            cleanup();
       }

       public void cleanup(){
         // perform some cleanup operations here.
       }
}

MainClass:- 
public class MainClass{
      public static void main(String args[]){
        Invoker invoker = new Invoker();
        invoker.invoke(“Dir”);     
     }
}

For more references:-
http://en.wikipedia.org/wiki/Command_pattern

No comments: