codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

21 / 100 topics
19Introduction to Behavioral Patterns20Chain of Responsibility Pattern21Command Pattern22Interpreter Pattern23Iterator Pattern24Mediator Pattern25Memento Pattern26Observer Pattern27State Pattern28Strategy Pattern29Template Method Pattern30Visitor Pattern33Practical Exercises for Behavioral Patterns
Tutorials/Design Patterns/Command Pattern
🎭Design Patterns

Command Pattern

Updated 2026-05-15
10 min read

Command Pattern

Introduction

The Command Pattern is one of the Behavioral Design Patterns that focuses on encapsulating a request as an object. This pattern allows you to parameterize clients with queues, requests, and operations, making it easier to manage and execute commands dynamically.

In simpler terms, the Command Pattern transforms a request into a stand-alone object that contains all information about the request. This includes the method name, the object that owns the method, and values for the method’s parameters. The transformation allows you to parameterize methods with different requests, delay or queue a request's execution, and support undoable operations.

Concept

The Command Pattern consists of several key components:

  1. Command: Declares an interface for executing an operation.
  2. ConcreteCommand: Implements the execute method by invoking a specific action on a receiver object.
  3. Receiver: Knows how to perform the operations associated with carrying out a request.
  4. Invoker: Asks the command to carry out the request.

Here's a simple diagram to illustrate these components:

+-------------------+
|    Invoker        |
|-------------------|
| - command: Command |
| + setCommand()     |
| + executeCommand()  |
+---------+----------+
          |
          v
+-------------------+
|   ConcreteCommand |
|-------------------|
| - receiver: Receiver |
| + execute()         |
+---------+----------+
          |
          v
+-------------------+
|    Receiver       |
|-------------------|
| + action()        |
+-------------------+

Examples

Basic Implementation

Let's start with a basic implementation of the Command Pattern. We'll create a simple remote control system where different commands can be executed.

Step 1: Define the Receiver

The receiver is an object that knows how to perform the operations associated with carrying out a request.

JavaScript
1class Light {
2 turnOn() {
3 console.log('Light is on');
4 }
5
6 turnOff() {
7 console.log('Light is off');
8 }
9}

Step 2: Define the Command Interface

The command interface declares an execute method.

JavaScript
1class Command {
2 execute() {
3 throw new Error("This method must be overridden by subclasses");
4 }
5}

Step 3: Implement Concrete Commands

Concrete commands implement the execute method by invoking a specific action on a receiver object.

JavaScript
1class LightOnCommand extends Command {
2 constructor(light) {
3 super();
4 this.light = light;
5 }
6
7 execute() {
8 this.light.turnOn();
9 }
10}
11
12class LightOffCommand extends Command {
13 constructor(light) {
14 super();
15 this.light = light;
16 }
17
18 execute() {
19 this.light.turnOff();
20 }
21}

Step 4: Define the Invoker

The invoker is an object that asks the command to carry out the request.

JavaScript
1class RemoteControl {
2 constructor(command) {
3 this.command = command;
4 }
5
6 setCommand(command) {
7 this.command = command;
8 }
9
10 executeCommand() {
11 this.command.execute();
12 }
13}

Step 5: Use the Command Pattern

Now, let's use the command pattern to control a light.

JavaScript
1const light = new Light();
2const lightOnCommand = new LightOnCommand(light);
3const lightOffCommand = new LightOffCommand(light);
4
5const remoteControl = new RemoteControl();
6
7remoteControl.setCommand(lightOnCommand);
8remoteControl.executeCommand(); // Output: Light is on
9
10remoteControl.setCommand(lightOffCommand);
11remoteControl.executeCommand(); // Output: Light is off

Advanced Example with Macro Commands

A macro command is a command that contains a list of commands. It can execute all the commands in its list.

Step 1: Define the Macro Command

JavaScript
1class MacroCommand extends Command {
2 constructor(commands) {
3 super();
4 this.commands = commands;
5 }
6
7 execute() {
8 this.commands.forEach(command => command.execute());
9 }
10}

Step 2: Use the Macro Command

Let's create a macro command to turn on the light and then turn it off.

JavaScript
1const macroCommand = new MacroCommand([lightOnCommand, lightOffCommand]);
2
3remoteControl.setCommand(macroCommand);
4remoteControl.executeCommand();
5// Output:
6// Light is on
7// Light is off

What's Next?

In the next section, we'll explore the Interpreter Pattern, which provides a way to evaluate language grammar or expression. This pattern is particularly useful for parsing and interpreting complex expressions.

Stay tuned!


PreviousChain of Responsibility PatternNext Interpreter Pattern

Recommended Gear

Chain of Responsibility PatternInterpreter Pattern