This is the sixth day of my participation in the August Text Challenge.More challenges in August

Definition of command pattern

In command mode, a method call, request, or operation is encapsulated in a single object to parameterize clients and pass executable method calls based on different requests. The command pattern gives us a means to separate responsibilities, including issuing commands from wherever they are executed and delegating that responsibility to different objects.

A command in command mode is an instruction to do something specific. The essence of the command pattern is to encapsulate these instructions, separating the responsibility of issuing the command from the responsibility of executing the command.

Each command is an operation: a requester makes a request to perform an operation; The receiver receives the request and performs the operation. The requester does not need to know the receiver’s interface or the steps to be performed in between.

Application scenarios of the command mode

The command mode is applicable to the following scenarios:

  • Requests need to be made at different times and queued for execution
  • You can also use the command mode if you need to support the command undo operation
  • The system needs to support macro commands, which require a group of operations to be combined

Advantages and disadvantages of command mode

Advantages:

  • Decouple the requester, the receiver, and the command object that implements the operation
  • Command object Command is a first-class object that can be extended
  • You can combine multiple commands into macro commands
  • Functions like undo operations can be achieved

Disadvantages:

  • Code implementation advantages around, more abstract

Concrete implementation of the code

// Control the TV practice code
var Command = function() {};

// Command receiver
var TV = function() {
    this.curChannel = 0;
    this.turnOn = function() {
        console.log('the tv is on');
    };
    this.turnOff = function() {
        console.log('the tv is off');
    };
    this.changeChannel = function(channel) {
        this.curChannel = channel;
        console.log('now the tv is ' + this.curChannel); }};// Start command
var CommandOn = function(tv) {
    Command.apply(this);
    this.tv = tv;
    this.execute = function() {
        this.tv.turnOn(); }};// Shutdown command
var CommandOff = function(tv) {
    Command.apply(this);
    this.tv = tv;
    this.execute = function() {
        this.tv.turnOff(); }};// Switch channel command
var CommandChange = function(tv, channel) {
    Command.apply(this);
    this.tv = tv;
    this.channel = channel;
    this.execute = function() {
        this.tv.changeChannel(this.channel);
    };
};

// Remote control as caller
var Control = function(open, close, change) {
    this.open = open;
    this.close = close;
    this.change = change;

    this.turnOn = function () {
        this.open.execute();
    };

    this.turnOff = function () {
        this.close.execute();
    };

    this.changeChannel = function () {
        this.change.execute();
    };
};

/ / TV
var tv = new TV();
// Generate respective commands
var open = new CommandOn(tv);
var close = new CommandOff(tv);
var change = new CommandChange(tv, 2);
/ / the remote control
var control = new Control(open, close, change);
control.turnOn();
control.turnOff();
control.changeChannel();
Copy the code