BehavioralTypeScriptverifiedVerified

Command Pattern in TypeScript

Encapsulates a request as an object, allowing you to parameterize clients, queue or log requests, and support undoable operations.

How to Implement the Command Pattern in TypeScript

1Step 1: Define the Command interface and receiver

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

class Counter {
  value = 0;
}

2Step 2: Implement concrete commands with undo support

class IncrementCommand implements Command {
  constructor(private counter: Counter, private amount: number) {}

  execute(): void {
    this.counter.value += this.amount;
  }

  undo(): void {
    this.counter.value -= this.amount;
  }
}

class ResetCommand implements Command {
  private previousValue = 0;

  constructor(private counter: Counter) {}

  execute(): void {
    this.previousValue = this.counter.value;
    this.counter.value = 0;
  }

  undo(): void {
    this.counter.value = this.previousValue;
  }
}

3Step 3: Create a command history for execute and undo

class CommandHistory {
  private history: Command[] = [];

  execute(command: Command): void {
    command.execute();
    this.history.push(command);
  }

  undo(): void {
    const command = this.history.pop();
    command?.undo();
  }
}

4Step 4: Execute commands and undo them

const counter = new Counter();
const history = new CommandHistory();

history.execute(new IncrementCommand(counter, 10)); // value = 10
history.execute(new IncrementCommand(counter, 5));  // value = 15
history.execute(new ResetCommand(counter));          // value = 0
history.undo();                                      // value = 15
history.undo();                                      // value = 10

Command Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Command Pattern in the Real World

Think of a restaurant order ticket. A waiter (invoker) takes your order and writes it on a slip (command). The slip is handed to the kitchen (receiver) which executes it. The waiter doesn't cook anything—they just carry and deliver orders. Tickets can be queued, cancelled before cooking, or reviewed in an audit log at day's end.