IT lexicon › Programming › Command pattern

Command pattern

Programming På svenska → Updated: 2026-10-25

A design pattern that wraps an action as an object, so it can be queued, undone or replayed after the fact.

Catalogued by the Gang of Four in Design Patterns (1994), it found perhaps its clearest home in games: Robert Nystrom's Game Programming Patterns (2014) opens with it. Instead of letting a key press call jump() directly, you create a JumpCommand — an object. The same code can then remap keys, feed the AI identical commands, and log them for playback.

The payoff is sharpest for undo/redo: a command that knows how to execute itself can also learn how to reverse itself. The input layer knows nothing about what the command does, only that it should run — a decoupling that also carries networked play, where the client ships commands rather than finished state.

← Back to the lexicon