Command Pattern

noun

say it: “kuh-MAND PAT-urn” /kəˈmænd ˈpætərn/

A design pattern that turns an action into an object, so it can be stored, queued, logged, run later and undone.

What is Command Pattern?

The Command pattern turns an action into an object, so it can be stored, queued, logged or undone. Text editors use it for undo and redo.

public interface ICommand { void Execute(); void Undo(); }

var history = new Stack<ICommand>();
var bold = new MakeBoldCommand(document, selection);
bold.Execute();
history.Push(bold);

In a sentence

  • “Once each assignment was an object, following the Command pattern, the dispatcher got an undo button almost for free.”

Level: Master · More OOP terms at this level

Updated