0Pricing
Java Academy · Lesson

Command Pattern: Encapsulating Actions

Wrap requests as Command objects to support undo/redo and macro recording.

The Command Intent

Command encapsulates a request as an object, allowing parameterization of clients with different requests, queuing, logging, and undoable operations.

Command Interface

Define an interface with an execute() method and optionally an undo() method. Each command object knows how to perform and reverse one action.

public interface Command {
    void execute();
    void undo();
}

All lessons in this course

  1. Observer Pattern: Event Notification
  2. Strategy Pattern: Interchangeable Algorithms
  3. Command Pattern: Encapsulating Actions
  4. Template Method: Defining Algorithm Skeletons
← Back to Java Academy