Skip to content
BBaskaran

Command

Turn a request into a stand-alone object that can be queued, logged or undone.

Use it when

You need undo/redo, job queues or to parameterise objects with operations.

/**
 * Command: turn each edit into an object, so the editor can undo it.
 *
 * Real-world scenario: a text editor with an Undo button. Typing and deleting are
 * wrapped in command objects that know how to do *and* undo themselves. The
 * editor runs commands and keeps them in a history stack; undo pops the last one
 * and reverses it. The editor never needs to know what each command does.
 */

// Receiver: the object that actually changes.
// (Named TextDocument because Document is a built-in browser type.)
class TextDocument {
  text = "";
}

// Command: one action, with everything needed to do and undo it.
interface Command {
  execute(): void;
  undo(): void;
}

class TypeText implements Command {
  private readonly doc: TextDocument;
  private readonly text: string;

  constructor(doc: TextDocument, text: string) {
    this.doc = doc;
    this.text = text;
  }

  execute(): void {
    this.doc.text += this.text;
  }

  undo(): void {
    const keep = this.doc.text.length - this.text.length;
    this.doc.text = this.doc.text.slice(0, keep);
  }
}

class DeleteText implements Command {
  private readonly doc: TextDocument;
  private readonly count: number;
  private deleted = ""; // remembered so undo can put it back

  constructor(doc: TextDocument, count: number) {
    this.doc = doc;
    this.count = count;
  }

  execute(): void {
    const keep = Math.max(this.doc.text.length - this.count, 0);
    this.deleted = this.doc.text.slice(keep);
    this.doc.text = this.doc.text.slice(0, keep);
  }

  undo(): void {
    this.doc.text += this.deleted;
  }
}

// Invoker: runs commands and keeps the history for undo.
class Editor {
  private readonly doc: TextDocument;
  private readonly history: Command[] = [];

  constructor(doc: TextDocument) {
    this.doc = doc;
  }

  run(command: Command): void {
    command.execute();
    this.history.push(command);
    console.log(`run:  ${JSON.stringify(this.doc.text)}`);
  }

  undo(): void {
    this.history.pop()?.undo();
    console.log(`undo: ${JSON.stringify(this.doc.text)}`);
  }
}

function clientCode(): void {
  const doc = new TextDocument();
  const editor = new Editor(doc);

  editor.run(new TypeText(doc, "Hello"));
  editor.run(new TypeText(doc, " world"));
  editor.run(new DeleteText(doc, 6));
  editor.run(new TypeText(doc, " there"));

  editor.undo(); // removes " there"
  editor.undo(); // brings back " world"
  editor.undo(); // removes " world"
}

clientCode();

// Output:
// run:  "Hello"
// run:  "Hello world"
// run:  "Hello"
// run:  "Hello there"
// undo: "Hello"
// undo: "Hello world"
// undo: "Hello"

How it works

RoleClassesWhat it does
ReceiverTextDocumentThe object that actually changes: it holds the text
CommandCommandInterface with execute() and undo()
Concrete commandsTypeText, DeleteTextEach knows how to do and undo one edit, remembering what it needs
InvokerEditorRuns commands and keeps a history stack; undo() reverses the last one
ClientclientCode()Creates commands and hands them to the editor