Design Pattern: Command Pattern

Summary

Pattern NameCommand Pattern
Pattern TypeBehavioral Pattern
ScopeObject
Other NamesAction
Transaction
TaglineWrap request in an object
Use cases1. When we want to create an object to store a command/request.
2. When we want to separate the separate request and execution of a command.
3. When we want to support features like undo or central logging of commands.
Related PatternsComposite
Memento
Difficulty LevelMedium
Implementations

Definition

Command pattern wraps a request/command in an object. Then we can pass the object anywhere and later when we want to execute the command the object can be used.

As the request is wrapped in an object, so the execution of the command is completely separate from the request sending part. Also, Command pattern can be used to support features like undoing and logging of command.

Implementation

Command pattern has 3 elements involved in the implementation.

  1. Command Interface: an interface(or abstract class) to ensure the common interface for all command classes.
  2. Concrete Command Classes: classes that implement the command interface and are responsible for executing command.
  3. Command Controller: class to take the command as param and executed the command. This controller can also contain additional functionality.

Take a look at the following diagram to understand the implementation of Command pattern.

Follow the steps below to implement Command pattern:

  1. Create an interface to ensure a common interface for all command classes.
  2. Create command classes and implement command interface.
  3. Create a command controller class. In this class create functions that take the command object as a param and execute functions/commands from that command object. This class can contain additional functions if required by the client.
  4. In the client create objects of command and pass that to the controller class functions for execution.

Examples

Example #1: UI Elements

Let’s take the example of printing UI elements.

Command Interface and Concrete Classes

// Command interface

interface UiCommand 

    print()

    remove()

end interface


// Class for handlign Input element

class InputUi implements UiCommand

    method print
        // Write code to print the element
        output "Printing Input"
    end method

    method remove
        // Write code for removing the element
        output "Removing Input"
    end method

end class


// Class for handling Button element

class ButtonUi implements UiCommand

    var final name: String

    constructor(nameParam: String) 
       name = nameParam
    end constructor

    method print
        // Write code to print the element
        output "Printing " + name + " Button"
    end method

    method remove()
        // Write code for removing the element
        output "Removing " + name + "  Button"
    end method

end class


// Class for handling Table element

class TableUi implements UiCommand
    
    method print
        // Write code to print the element
        output "Printing Table"
    end method

    method remove
        // Write code for removing the element
        output "Removing Table"
    end method

end class

Controller

class UiControl

    // Array to hold the list of executed commands
    var private commandList[]

    method addElement(UiCommand command) {
        // Execute command
        command.print()

        // Store command in list to have a history
        commandList.add(command)
    end method

    public void removeElement(UiCommand command) {
        // Remove element
        command.remove();

        // Store command in list to have a history
        commandList.remove(command)
    end method

    public void undo() {
        // Get the last element
        var lastElementIndex = commandList.size() - 1
        var lastCommand = commandList.get(lastElementIndex)
        
        // Remove last element
        removeElement(lastCommand)
    end method

end class

Demo

var uiControl = new UiControl()

var inputUi = new InputUi()
var tableUi = new TableUi()
var buttonUi = new ButtonUi("Submit")

uiControl.addElement(inputUi)
uiControl.addElement(tableUi)
uiControl.addElement(buttonUi)

// Remove specific element
uiControl.removeElement(tableUi)

// Add some new elements
uiControl.addElement(new ButtonUi("Cancel"))
uiControl.addElement(new TableUi())
uiControl.addElement(new InputUi())
uiControl.addElement(new ButtonUi("Wrong button"))

// Undo last to command
uiControl.undo()
uiControl.undo()

Output

Printing Input
Printing Table
Printing Submit Button

------------------------------------

Removing Table

------------------------------------

Printing Cancel Button
Printing Table
Printing Input

------------------------------------

Printing Wrong button Button
Removing Wrong button  Button
Removing Input

Code Implementations

Use the following links to check Command pattern implementation in specific programming languages.

Leave a Comment