Skip to content
Design Patterns

Command

Purpose

The Command Pattern’s primary purpose is to decouple sender and receiver in a way that allows multiple requests to be handled in a flexible and extensible manner. By encapsulating a request in an object, it enables parameterization of clients with different requests and supports undoable operations.

Context

The Command Pattern is most useful in scenarios where you have an object that needs to send requests to perform actions on other objects, but you want to decouple the sender and receiver. This pattern is commonly used in GUI applications, multi-level undo/redo functionalities, transaction systems, and other scenarios where request handling and execution need to be managed in a flexible and extensible manner.

Participants

  1. Command: Interface or abstract class that declares an execute() method, which represents the action to be performed.
  2. ConcreteCommand: Implements the Command interface, binding a specific action to a receiver. It knows the receiver and invokes the appropriate operations.
  3. Client: Initiates the request to execute a particular command.
  4. Invoker: Asks the command to carry out the request.
  5. Receiver: Knows how to perform the operations associated with the command.

Flow

  1. The client creates a specific Command object and sets its receiver (the object that will perform the actual action).
  2. The client then passes the Command object to the Invoker.
  3. The Invoker stores the Command object (optional) and triggers the execute() method of the Command.
  4. The Command object invokes the corresponding operation on the Receiver.
  5. The Receiver performs the requested action.

Real World Scenarios

  1. GUI Applications: Using the Command Pattern to handle user interactions and actions in graphical user interfaces.
  2. Undo/Redo Functionality: Implementing multi-level undo and redo operations in software applications.
  3. Transaction Management: Managing database transactions and supporting rollbacks in database systems.
  4. Remote Control Systems: Creating remote controls that can execute various commands on electronic devices.
  5. Game Controls: Handling player actions and commands in video games.
  6. Document Editing: Supporting undo, redo, and history tracking in text editors or word processors.
  7. Batch Processing: Queuing and executing batch commands in data processing systems.
  8. IoT Devices: Enabling command handling for controlling IoT devices remotely.
  9. Virtual Assistants: Implementing voice command processing in virtual assistant applications.
  10. Version Control Systems: Managing versioned operations and changes in code repositories.
  11. Robotics: Controlling robot actions and tasks using command objects.
  12. Home Automation: Managing various home automation actions through commands.
  13. Network Communication: Handling and executing network protocols and messages.
  14. Video Editing Software: Supporting non-linear video editing with undo and redo capabilities.
  15. Smart TVs: Implementing remote control functions for smart TVs and entertainment systems.
  16. Automated Testing: Using the Command Pattern to design and execute automated test cases.
  17. Task Scheduling: Queuing and executing scheduled tasks in software systems.
  18. Drawing and Graphics Applications: Enabling user actions to draw and manipulate graphics objects.
  19. Customer Support Systems: Managing and executing customer support actions and workflows.
  20. Automated Home Systems: Integrating commands for home security, lighting, and climate control.

Mnemonic

“Command Control”

Explanation:

“Command”: Represents the Command Pattern, taking control over requests and actions by encapsulating them in objects, just like a commander leading his troops.

“Control”: Denotes the management of commands by the Invoker, orchestrating the execution of commands and maintaining order and flexibility.

Imagine a “Command Control” scenario where the Command Pattern acts as the commander, guiding and organizing different commands (ConcreteCommands) from various sources (Clients) to perform actions. Each command fulfills its mission, but they all follow the lead of the Command Pattern. The control center (Invoker) ensures seamless coordination, enabling new commands to join the operation without disturbing the established flow, creating a harmonious and efficient command structure.