← All Quizzes
What is the Command pattern and what features does it enable?
Explanation
The Command pattern encapsulates a request as an object, containing the receiver and action to perform. This decouples the requester from the receiver and enables: queuing requests, logging operations, undo/redo (by storing inverse operations), macro recording, and deferred execution. Commands have a common interface (execute, undo), making them interchangeable. Use cases: text editors (undo/redo), transaction management, job queues, and UI button actions. The pattern transforms method calls into first-class objects, enabling powerful control over execution.