Also known as

Command

Intent

Encapsulate a request as an object, thereby letting you parametrize clients with different requests, queue or log requests, and support undoable operations.

Structure of design pattern

Command pattern structure

Pseudocode

// Abstract command defines the common interface for all concrete commands.
abstract class Command is
    field app: Application
    field editor: Editor
    field backup: text

    constructor Command(app: Application, editor: Editor) is
        this.app = app
        this.editor = editor

    // This abstract command defines undo functionality. To save more complex
    // editor state, you might need to use the Memento pattern.
    method backup() is
        backup = editor.text
        app.history.push(this)

    method undo() is
        editor.text = backup

    // Main command method stays abstract so that each concrete command could
    // provide its own implementation.
    abstract method execute()


// Concrete commands.
class CopyCommand extends EditorCommand is
    // The copy operation is not saved to the history since it does not change
    // editor's state.
    method execute() is
        app.clipboard = editor.getSelection()

class CutCommand extends EditorCommand is
    method execute() is
        // Commands, which change editor's state, will be saved to the history.
        backup()
        app.clipboard = editor.getSelection()
        editor.deleteSelection()

class PasteCommand implements Command is
    method execute() is
        backup()
        editor.replaceSelection(app.clipboard)


// Global command history is just a stack.
class CommandHistory is
    history: array of Command

    // Last in...
    method push(c: Command) is
        Push command to the end of history array.

    // ...first out
    method pop():Command is
        Get the most recent command from history.


// Editor class has an actual text editing operations. It plays the role of
// Receiver: all commands end up delegating execution to editor's methods.
class Editor is
    field text: string
    field cursorX, cursorY, selectionWidth

    method getSelection() is
        Return selected text.

    method deleteSelection() is
        Delete selected text.

    method replaceSelection(text) is
        Insert clipboard contents at current position.


// Application class configures object relations. It acts as a Sender–creates
// and sends command objects to execute some action.
class Application is
    field clipboard: string
    field editors: array of Editors
    field activeEditor: Editor
    field history: CommandHistory

    method createUI() is
        onKeyPress("Ctrl+C", this.getCopyCommand);
        onKeyPress("Ctrl+X", this.getCutCommand);
        onKeyPress("Ctrl+V", this.getPasteCommand);
        onKeyPress("Ctrl+Z", this.undo);

    // Hotkeys handler creates new command object each time it fires. In this
    // case, commands could work with multiple editors, but they have the
    // common clipboard.
    method getCopyCommand() is
        return (new CopyCommand(this, activeEditor)).execute()
    method getCutCommand() is
        return (new CutCommand(this, activeEditor)).execute()
    method getPasteCommand() is
        return (new PasteCommand(this, activeEditor)).execute()

    // Take the last command from history and run its undo method. Note that we
    // don't know the type of that command. But we don't have to since command
    // knows how to undo its own actions.
    method undo() is
        command = history.pop()
        if (command != null)
            command.undo()

Implementations in Different Programming Languages

Java