It is a behavioral design pattern that turns a request into an object. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations. It decouples the sender (who wants something done) from the receiver (who actually performs the action).
Used when you want to queue operations, schedule their execution, or execute them remotely. A command can be serialized, which means converting it to a string that can be easily written to a file or a database. You can delay and schedule command execution. you can put them in queue, log or send them over the network.
Used when you want to implement undoable actions, e.g text editor. you turn your operations into a command and then push them in a stack which you can pop out to replay previous commands.
- Command Interface – Defines the contract for executing an operation.
- Concrete Commands – Implement the command interface and define actions.
- Invoker – Stores and invokes commands when needed.
- Receiver – The actual entity that performs the task.
- Handling undo/redo actions in Text Editor.
- Managing player actions in Game Engine.
- Task Scheduling via queues.