Command Pattern: Structure & Participants
Pattern Structure
Participant Roles
Command Interface: Declares execute() and undo() methods that all concrete commands must implement. This is the contract that allows invokers to work with any command without knowing specifics.
ConcreteCommand: Implements the Command interface and holds a reference to the Receiver. It stores any state needed for undo (parameters, previous values) and delegates actual work to the Receiver. The command knows what to do but the Receiver knows how to do it.
Receiver: The object that performs the actual business logic. It could be a Document, TextEditor, or BankAccount. The Receiver has no knowledge of commands and would work fine without the pattern.
Invoker: Triggers command execution without knowing what the command does. A Button, MenuItem, or CommandQueue holds a Command reference and calls execute() when appropriate.
Client: Creates ConcreteCommand objects and sets their Receiver. The client wires everything together but doesn't execute commands directly.
Execution Flow
First, the Client creates a ConcreteCommand and configures it with a Receiver. Second, the Client passes the command to an Invoker. Third, when triggered, the Invoker calls command.execute(). Fourth, the ConcreteCommand calls methods on its Receiver to perform the actual work. For undo, the Invoker calls command.undo(), and the command restores previous state using stored information.