Command Pattern in Java

Command Design is one of the Behavioral design patterns.

When Would I Use This Pattern?

  • A history of requests is needed (Text editor undo, redo)
  • Requests need to be handled at variant times or in variant orders (We keep commands queued up)
  • The invoker should be decoupled from the object handling the invocation.(ie Light should be loosely coupled from the ON/OFF switch)

Code Findings:

  • In games, we can configure(customize) buttons. There, buttons have onClickListener(), which has a ICommand. So now you can configure your A,S,D,W dynamically.
  • In databases, we can have actions to Do & Undo operations. This is done via ICommands.
  • In medium editor (or any text editor), all actions are commands. That is why you have options for undo, redo.

Quora Excerpt: [Ref]

  • Graphic User Interface (GUI): In GUI and menu items, we use command pattern. By clicking a button we can read the current information of GUI and take an action.
  • Macro Recording: If each of user action is implemented as a separate Command, we can record all the user actions in a Macro as a series of Commands. We can use this series to implement the “Playback” feature. In this way, Macro can keep on doing same set of actions with each replay.
  • Multi-step Undo: When each step is recorded as a Command, we can use it to implement Undo feature in which each step can by undo. It is used in text editors like MS-Word.
  • Networking: We can also send a complete Command over the network to a remote machine where all the actions encapsulated within a Command are executed.
  • Progress Bar: We can implement an installation routine as a series of Commands. Each Command provides the estimate time. When we execute the installation routine, with each command we can display the progress bar..
  • Transactions: In a transactional behavior code there are multiple tasks/updates. When all the tasks are done then only transaction is committed. Else we have to rollback the transaction. In such a scenario each step is implemented as separate Command.

Example Game:

ASDW used to control the car.

Suppose I creating this car game. I want players to configure button actions. We can give this freedom of choice, via Command Pattern.

Project Structure:

Implementation in Java

Button.java

GameCharacter.java

2. Now we have to create Commands based on things, that invokee can perform.

ICommand.java

MoveDownCommand.java

Note:

  • Invokee is passed in the command via constructor
  • unexecute() is negation of execute()

MoveUpCommand.java

3. The Driver class to test this.

Found it Interesting?
Please show your support by 👏. To read the complete series, click below.

Disclaimer:
I myself, has just started learning, design patterns. If you find any issues please feel free to post them in the comments section below. Thank you for reading so far 😄

Writes on Big Data, AWS & Backend technologies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store