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. public enum Action
  3. {
  4. DOOR_OPEN,
  5. DOOR_CLSOE
  6. }
Below is the ICommand interface which will define contact to implement
  1. package com.sample.Command.Design.Pattern;
  2. public interface ICommand {
  3. void execute(Action action);
  4. void undoExecute();
  5. }
Below is the DoorCommand class which implements the ICommand contract
  1. package com.sample.Command.Design.Pattern;
  2. public class DoorCommand implements ICommand {
  3. private Door door;
  4. Action lastAction;
  5. public DoorCommand()
  6. {
  7. door = new Door();
  8. }
  9. public void execute(Action doorAction)
  10. {
  11. switch (doorAction)
  12. {
  13. case DOOR_OPEN:
  14. door.open();
  15. break;
  16. case DOOR_CLSOE:
  17. door.close();
  18. break;
  19. default:
  20. return;
  21. }
  22. lastAction = doorAction;
  23. }
  24. public void undoExecute()
  25. {
  26. switch (lastAction)
  27. {
  28. case DOOR_OPEN:
  29. door.close();
  30. break;
  31. case DOOR_CLSOE:
  32. door.open();
  33. break;
  34. default:
  35. return;
  36. }
  37. }
  38. }
Below is the Door class, it actually has core logic implementation
  1. package com.sample.Command.Design.Pattern;
  2. public class Door {
  3. public void open() {
  4. System.out.println("------------Door Opened---------------");
  5. }
  6. public void close(){
  7. System.out.println("------------Door Closed---------------");
  8. }
  9. }
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. public class Main {
  7. public static void main(String[] args) {
  8. ICommand command = new DoorCommand();
  9. command.execute(DOOR_OPEN);
  10. command.execute(DOOR_CLSOE);
  11. command.undoExecute();
  12. }
  13. }
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.