The applicability and examples of the command pattern are as follows:
- Undo/redo operations: The command pattern allows us to store the command object in a queue. This way, we can implement undo and redo operations.
- Composite commands: Complex commands can be composed of simple commands using the composite pattern, and are run in a sequential order. In this way, we can build macros in an object-oriented-design manner.
- The asynchronous method invocation: The command pattern is used in multithreading applications. Command objects can be executed in the background in separate threads. The java.lang.Runnable is a command interface.
In the following code, the runnable interface acts as a command interface, and is implemented ...