Tuesday, September 16, 2014

Command Pattern in Java

This is the story of Antony "The ruthless mafia boss". Antony is ruling city mafia for over 3 decades now. Every crime in the city happens with his orders.
He has 4 Gangs working under him. He takes care that the authorities, police etc. are under control. Now he is 65 years old but still he has not lost his control. Although he don't go into the details of each and every crime and its execution but his approval is must.
Every morning people assemble in his courtyard with their requests. They meet his son who looks into all the details and decide if the request will go further and to which gang. For such requests he creates a letter with all the required information and then they take this letter to Antony. This letter has all the details required for the execution of crime.
Antony never opens the envelope. If situation is favourable. Connections with Police and all other authorities are under control he just says "DO IT". Then the letters reaches the Gangs who execute the orders.

Nice story, but the title of this article is 'Command Pattern' and not 'Godfather Returns'. So, time to put on our Java glasses and read the story again. 
I told you this story as Antony has been using Command Pattern to run his mafia successfully since ages. 

The Gangs (Executors)

Although they don't seem to be very important in the story but they are the ones who execute everything and they know "How to do it?".
Here are the 3 Gangs which specialize in different types of crimes.

Here is sample Gang Class-


These Gangs only wait for commands(Letter) from Antony to do the work.
The Letter
Letter plays a very important role it is a command for the Gangs. And there could be different types of commands.

  • MurderCommand
  • KidnapCommand
  • StealCommand ....And More










How can a Letter reach the destination without address? 
So, Letter should have information about the Gang where it should go.

Command object should have the reference of the Executor Object (Gang object).
Which will do the real execution.

So,  MurderCommand has Gang3 object. It calls murder() method of Gang3 from its execute() method.
                                                              Command has Gang




In this case calling a single method murder() is enough for execution. 
But in some cases Command might need to call mutiple methods of one or more executors. 

Example- There could be a Macro command to steal, kidnap & murder.
So, execute() method will call steal(), kidnap() and murder() method of Gang1, Gang2 & Gang3 objects.

Letter is the part which travels from beginning till end of this story and thus makes it possible
Command Object helps in separating the creation and execution logic.
Antony (The Invoker)-
He is the most important character of this story. As nothing can happen without him. He is the one who decides when the command will be executed(remember DO IT). He is the invoker of command.
So, To murder someone Antony don't need to know how a murder is done(Although he knows). he just need to say DO IT and rest is done.

In Java terms to execute the command invoker just need to have Command object whose execute() method it will call to Invoke the command.

Command pattern separates the logic of creating a request and executing a request.


  
Antony has Command



Requestors (or Clients)

These are the people who came to Antony for getting their work done. Their requests actually changed to commands when Antony said DO IT.
Their work is to give all the details required for the execution in the letter. And to hand it over to Antony. The story starts from them. 
Client creates the Command object and then set this object in Invoker using setter method. 
Full Picture

 Command Pattern Class Diagram

In this case Antony does not want the command to complete till he is sure that everything is under control. So, although creation of letter happens early but its execution happens later.

Command Pattern is required when the command can't be executed as soon as it is created. And there is a separate entity or condition which controls the invocation of command(Antony in this case).

There could be more than one letter with antony when he decides time is appropriate for execution.

Invoker can have more than one command.

























visit ApexInformatix.com for Online/Classroom course on Java and other technologies