Command Design is one of the Behavioral design patterns.
When Would I Use This Pattern?
The Command Pattern is useful when:
- 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)
- This is similar to actions and reducers in react-redux. Actions are invokers. They reach out to reducers, for state change. The invokee might be the Media Object playing a sound. [Just a thought]
- 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]
Command design pattern is a behavioral design pattern. We use it to encapsulate all the information required to trigger an event. Some of the main uses of Command pattern are:
- 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.
Suppose I creating this car game. I want players to configure button actions. We can give this freedom of choice, via Command Pattern.
Implementation in Java
- Let us start with the visual/physical things first: invoker and the invokee.
2. Now we have to create Commands based on things, that invokee can perform.
- Invokee is passed in the command via constructor
- unexecute() is negation of execute()
3. The Driver class to test this.
Found it Interesting?
Please show your support by 👏. To read the complete series, click below.
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 😄