Command Design Pattern Using Java Sample

What is Command Design Pattern?

 
Command Design pattern is a behavioral design pattern in which a request can be turned to action.
 

Where to use Command Design Pattern?

 
When we have a requirement of,
  • Do and undo action
  • When you want to pass method as argument or in another way when you want to call callbackfunction
  • When we want to log or track commands

Why use Command Design Pattern?

 
Command Design Pattern is a request driven design pattern. A request will help to choose which command needs to be executed without knowing which object.method to call. In this kind of case, command design pattern plays a key role which will make this easy by using ICommand. Command design pattern actually decouples command manager and command execution logic; i.e. Actual implementation.
 
Players in this pattern
  • ICommand – an interface which will do action and undo action
  • ConcreateCommand –  implements ICommand
  • Invoker – carries out the action
  • Receive -- receives action from invoker and performs action

Problem definition

 
Provide interface to user to open and close the door. And if user want,s user should be able to undo his/her last action.
 
This example actually showcases the use case of do and undo action requirement. For other requirements I will write separate articles.
 
Players here,
  • ICommand – ICommand
  • ConcreateCommand – DoorCommand
  • Invoker – client, in our case  client operation in main function
  • Receiver – Door
We will first define actions which we want to execute,
  1. package com.sample.Command.Design.Pattern;  
  2.   
  3. public enum Action  
  4. {  
  5.     DOOR_OPEN,  
  6.     DOOR_CLSOE  
  7. }  
Below is the  ICommand interface which will define contact to implement 
  1. package com.sample.Command.Design.Pattern;  
  2.   
  3. public interface ICommand {  
  4.     void execute(Action action);  
  5.     void undoExecute();  
  6. }  
Below is the DoorCommand class which implements the ICommand contract
  1. package com.sample.Command.Design.Pattern;  
  2.   
  3. public class DoorCommand implements  ICommand {  
  4.     private Door door;  
  5.     Action lastAction;  
  6.   
  7.     public DoorCommand()  
  8.     {  
  9.         door = new Door();  
  10.     }  
  11.   
  12.     public void execute(Action doorAction)  
  13.     {  
  14.         switch (doorAction)  
  15.         {  
  16.             case DOOR_OPEN:  
  17.                 door.open();  
  18.                 break;  
  19.             case DOOR_CLSOE:  
  20.                 door.close();  
  21.                 break;  
  22.             default:  
  23.                 return;  
  24.         }  
  25.         lastAction = doorAction;  
  26.     }  
  27.   
  28.     public void undoExecute()  
  29.     {  
  30.         switch (lastAction)  
  31.         {  
  32.             case DOOR_OPEN:  
  33.                 door.close();  
  34.                 break;  
  35.             case DOOR_CLSOE:  
  36.                 door.open();  
  37.                 break;  
  38.             default:  
  39.                 return;  
  40.         }  
  41.     }  
  42. }  
Below is the Door class, it actually has core logic implementation
  1. package com.sample.Command.Design.Pattern;  
  2.   
  3. public class Door {  
  4.     public void open() {  
  5.         System.out.println("------------Door Opened---------------");  
  6.     }  
  7.     public void close(){  
  8.         System.out.println("------------Door Closed---------------");  
  9.     }  
  10. }  
Below is the Invoker, which requests for some action 
  1. package com.sample;  
  2. import com.sample.Command.Design.Pattern.DoorCommand;  
  3. import com.sample.Command.Design.Pattern.ICommand;  
  4. import static com.sample.Command.Design.Pattern.Action.DOOR_CLSOE;  
  5. import static com.sample.Command.Design.Pattern.Action.DOOR_OPEN;  
  6.   
  7. public class Main {  
  8.   
  9.     public static void main(String[] args) {  
  10.         ICommand command = new DoorCommand();  
  11.         command.execute(DOOR_OPEN);  
  12.         command.execute(DOOR_CLSOE);  
  13.         command.undoExecute();  
  14.     }  
  15. }  
Below is the output Snap
 
Command Design Pattern Using Java Sample
 

Summary

 
In this article we understood about usage of command design pattern mainly concentrating on do and undo operations.