The Command Design Pattern

The Command pattern encapsulates a request as an object, thereby allowing for parameterization of clients with different requests.

Use the Command pattern when you want to decouple the sender and receiver of a request, or when you want to parameterize objects with operations.

  • TypeBehavioral
  • Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
  • Efficiency6
  • Learning Effort

    • Average6
    • Conceptual7
    • Debug and Maintain5
    • Implementation6
    • Prerequisites5
    • Versatility8

Things you should know before you start

  • Encapsulation Understanding of encapsulation, as the command object encapsulates everything about the request.
  • Polymorphism Understanding of polymorphism, as the command pattern relies on object substitution to decouple senders from receivers.

Minimal Example

Implementing an undo-redo feature in a text editor.

class UndoCommand implements Command {
  execute() {
    // Undo last action
  }
}
const undo = new UndoCommand();
const invoker = new Invoker();
invoker.setCommand(undo);
invoker.executeCommand();

What’s happening in thie example

In this example, the `UndoCommand` class is a ConcreteCommand that implements the Command interface. The `Invoker` class is used to execute these commands. The `clientCode` shows how to set and execute the Undo command.

How the Command Works

The Command pattern turns a request into a stand-alone object containing information about the request. This decoupling allows for parameterization and queuing of requests, and provides additional functionalities like logging and caching.

This pattern is useful in GUIs, multi-level undo-redo features, and macro recording.

Entities in the Command Design Pattern

  • CommandThe Command interface declares a method for executing a particular action.

    • Command has a one-to-many implements relationship with ConcreteCommand

What to watch out for when using or considering this design pattern.

Avoid using the Command pattern when you have a limited set of simple actions that don’t require additional functionalities like undo/redo, logging, or queuing.

Design patterns often confused with the Command Design Pattern

The following design patterns are often confused with the Command
for various resons.

If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.

  • Strategy – The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.

    The Strategy pattern is similar to the Command in the following ways.

    • Both are behavioral design patterns.
    • Both encapsulate algorithms or operations.
  • Here are some notable differences with the Command pattern.

    • Command encapsulates a request as an object, whereas Strategy encapsulates an algorithm.
    • Command objects can be queued and logged, while Strategy objects typically cannot.
  • Chain of Responsibility – The Chain of Responsibility pattern passes a request along a chain of handlers.

    The Chain of Responsibility pattern is similar to the Command in the following ways.

    • Both are behavioral design patterns.
    • Both deal with encapsulating requests.
  • Here are some notable differences with the Command pattern.

    • Command focuses on encapsulating a request as an object, while Chain of Responsibility focuses on passing the request along a chain.
    • In Command, handlers are not linked, whereas in Chain of Responsibility, they are.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top