Tuesday, January 24, 2012

Going Technical....Command Pattern

I am back again with a different pattern today. The name of the pattern that we are going to look at today is Command pattern. Usually this pattern is very famous as well as sometimes it makes us confused as well, anyway let's try to look at an analogy of this pattern in real life.

Back then in the world , there were wars between nations for something valuable , sometimes they waged wars on acquiring a piece of land or for some other resource. Whatever the reason we know that a war is not pleasant , but for the sake of understanding the pattern let's assume that two parties were in a war and this happened during the times that they had only swords and horses as weapons and transportation.

During these times if some party had wanted to send a command to it's ally about what should have been the next action, the leader of the group would have written the command in a piece of paper ,possibly would have encrypted in a certain way and would have sent this through a trustworthy person.

Once this command had reached the ally the message would have been received by the leader of the ally and he would have ordered his warriors to act according to the command. In this case there was no need for the two leaders to know each other personally and they did not directly communicate to each other either. Also the first leader could have sent more than one message written in more than one papers. In such case the person who had been delivering the message to the other ally had had the privilege of passing the messages in a certain order , possibly following the instructions from the first leader.

Now it is a good time for us to look at command pattern in detail to have a better understanding on how the example and the pattern go together. Command pattern may be used in situations like explained above. If you need to decouple a sender and a receiver and encapsulate command related logic elsewhere , Command pattern is a good choice. Also this help in delayed or timed execution of a particular command as well as queuing the commands based on a certain set of rules.

In Command pattern , there are few actors involved ,

1. Sender
2. Receiver
3. Invoker
4. Command itself

If we take above analogy , the Sender would be the leader of the party , Receiver would be the leader of the ally, Invoker would be the warriors who were going to act , and the Command itself is the message written in the piece of paper. In this case we see that the Sender and the Receiver is decoupled and also the Sender could send more than one piece of paper containing completely different command as well. In such case Warriors would act on each command according to it's assigned priority. That means Command pattern would also help you queue more than one command and execute based on some sequence.

Lets try understanding some code now,

public class PartyLeader{

  private Command commandToAlly;
  //initiate a command to be sent to the Ally
  public void initiateCommand(){
    commandToAlly = new MoveToEast();

  public Command getCommandToAlly(){
    return this.commandToAlly;

public class AllyLeader{

  private Command commandFromParty;

  public void receiveCommandFromParty(Command commandFromParty){
    this.commandFromParty = commandFromParty;
  public void commandWarriors(){
    Warriors warriorsUnderAllyLeader = new Warriors();

public class Warriors{
  public void moveToEast(){
  public void moveToNorth(){
  public void moveToSouth(){
  public void moveToWest(){

public interface Command{
  void execute(Warriors warriors);

//Command sent using a paper to the ally
public class MoveToEast implements Command{
  public void execute(Warriors warriors){

//A main method to see all actors in action
public static void main(String args[]){

  //The leader of the party is initiating the command to Ally
  PartyLeader leaderOfTheParty = new PartyLeader();
  //This is as if the leader of the party writes the command in a piece of
  //paper and sending it via a messenger
  Command commandToAlly = leaderOfTheParty.getCommandToAlly();

  //now that the leader of the Ally
  AllyLeader leaderOfTheAlly = new AllyLeader();
  //Leader of the ally receives the command from the messenger and probably reads it
  //Then the leader of the Ally instructs his warriors what should be done

It is clear that the Sender (PartyLeader) does not have any idea about the Receiver (AllyLeader) , neither sender knows who is going to execute the command. And the Receiver does not know about the Sender , but knows what to do with the command. The actual work is done by the Invoker , (i.e Warriors in this case). By using a Command Sender and Receiver are decoupled well.

Note: Command pattern sometimes confuses us , if you have questions you may place a comment for which I could help answering...:)

No comments: