Open In App

Command Method | JavaScript Design Patterns

The command method is a behavioral design pattern that encapsulates an incoming request into a standalone object. This object contains all the necessary information to perform a request including the method to call and parameters.

Why do the we use Command Method?

The Command Method is needed to separate the sender information of the request and the object that processes the request. It promotes loose coupling, reusability, and flexibility in handling commands by turning requests into standalone objects. So, the objects can be processed or executed by the different parts of the application.

Command Method example in JavaScript Design Patterns

problem statement



We will implement control of a light bulb’s state (On or Off) using an organized and flexible structure. The system should allow the client to interact with the light bulb via commands and provide a simple and efficient way to switch the light on and off.

Implementation of Command Method using JavaScript:

1. Creation of Command class

Note: JavaScript doesn’t support Interfaces directly. Create a class with empty implementation and override the method in other class.




class Command {
  execute() {}
}

In this step, We have created the command class which contains the declaration of the execute method.

2. Creation of Concrete Command class




class OnCommand extends Command {
  constructor(lightBulb) {
    super();
    this.lightBulb = lightBulb;
  }
 
  execute() {
    this.lightBulb.on();
  }
}
 
class OffCommand extends Command {
  constructor(lightBulb) {
    super();
    this.lightBulb = lightBulb;
  }
 
  execute() {
    this.lightBulb.off();
  }
}


3. Creation of Receiver

In this step, We have created two classes namely `OnCommand` and `OffCommand` which extend the `Command` class. Each class contains a variable `lightBulb` which holds the information i.e. `On` or `Off ` status. These classes override the `execute` method of the command class and define it with command-specific implementation.




class LightBulb {
  on() {
    console.log('Bulb is ON');
  }
 
  off() {
    console.log('Bulb is OFF');
  }
}

4. Creation of Invoker

In this step, We have created the `Receiver` class which is `LightBulb`, which contains the actual implementation of the `on` and `off` methods.




class Switch {
  constructor() {
    this.command = null;
  }
 
  setCommand(command) {
    this.command = command;
  }
 
  executeCommand() {
    this.command.execute();
  }
}


In this step, We have created the Invoker which is a `Switch` class that defines a constructor that initializes the command to a null value at first. The Switch class defines two methods namely `setCommand` and `executeCommand` whose purpose is to set the command (on or off) and execute the command (the console logs the information).

5. Usage and Client code

The client code interacts with the `Concrete Command` class, `Receiver` and `Invoker`. The `Invoker (Switch)` sets the command and executes the command accordingly.




const light = new LightBulb();
const on = new OnCommand(light);
const off = new OffCommand(light);
 
const switchButton = new Switch();
 
switchButton.setCommand(On);
switchButton.executeCommand();
 
switchButton.setCommand(off);
switchButton.executeCommand();

Below is the complete combined code for the above example:




class Command {
  execute() {}
}
class OnCommand extends Command {
  constructor(lightBulb) {
    super();
    this.lightBulb = lightBulb;
  }
 
  execute() {
    this.lightBulb.On();
  }
}
 
class OffCommand extends Command {
  constructor(lightBulb) {
    super();
    this.lightBulb = lightBulb;
  }
 
  execute() {
    this.lightBulb.Off();
  }
}
class LightBulb {
  On() {
    console.log('Bulb is ON');
  }
 
  Off() {
    console.log('Bulb is OFF');
  }
}
class Switch {
  constructor() {
    this.command = null;
  }
 
  setCommand(command) {
    this.command = command;
  }
 
  executeCommand() {
    this.command.execute();
  }
}
const light = new LightBulb();
const On = new OnCommand(light);
const Off = new OffCommand(light);
 
const switchButton = new Switch();
 
switchButton.setCommand(On);
switchButton.executeCommand();
 
switchButton.setCommand(Off);
switchButton.executeCommand();

Output
Bulb is ON
Bulb is OFF


Components of Command Method

The components of the Command Method include

Diagrammatic Representation of Command Method:

Below is the explanation of the above diagram:

In this diagram,

Advantages of the Command Method in JavaScript Design Patterns

Disadvantages of the Command Method in JavaScript Design Patterns


Article Tags :