JavaScript Design Pattern Command Pattern

JavaScript Design Pattern Command Pattern

The command pattern is a behavioral design pattern in JavaScript design patterns;

Definition: Send a request to some object, but don't know what the specific operation is, so we want to design the program in a loosely coupled way so that the request sender and the receiver can eliminate the coupling relationship between each other; and our loose coupling method is the command pattern;

Plain language explanation: If you are the team leader of your company's R&D department, and your leader assigns you a task, you take a quick look and find that very simple requirements are relatively easy to implement; and as a team leader, you will definitely have a lot of things to do every day, so you are prepared to throw the requirements directly to the team members to develop and implement; the leader does not care whether it is you who does it or who you ask to do it, the leader only wants the final results! Here the leader is the issuer of the order, and you are the receiver of the order;

Code implementation:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <button id="button1">Publish command to front end</button>
        <button id="button2">Publish command to the backend</button>
</body>
<script>

    var button1 = document.getElementById("button1");
    var button2 = document.getElementById("button2");
    //Define command var command = function(Executor,func){
        Executor.onclick = func;
    }
    // Define leader var Leader = {};
    
    Leader.teamleader = {
        web:function(){
            console.log("The front end will be completed soon");
        },
        java:function(){    
            console.log("Background will be completed soon")
        }
    }

    command(button1,Leader.teamleader.web);
    command(button2,Leader.teamleader.java);
</script>
</html>

Running results:

Here, the command object is defined as a method to perform different tasks according to the parameters. When you click different buttons, different commands are executed;

Macros:

A macro command is a collection of commands. By executing macro commands, a batch of commands can be executed at one time.

Computer startup items: Many software now have default startup items added to the computer startup, which means that certain specific software will be started by default after our computer is turned on; this is a scenario of macro commands;

var QQCommand = {
    execute:function(){
        console.log("self-starting QQ successfully");
    }
}

var weChatCommand = {
    execute:function(){
        console.log("WeChat started successfully");
    }
}

var MacroCommand = function(){
    return {
        list:[],
        add:function(command){
            this.list.push(command);
        },
        execute:function(){
            for(var i = 0,command;command = this.list[i++];){
                command.execute();
            }
        }
    }
}

var macroCommand = MacroCommand();
macroCommand.add(QQCommand);
macroCommand.add(weChatCommand);
macroCommand.excute();

In the above code, we define a list array in the macro command object, and then add it to the execution queue through the add method. The so-called execution queue is the list array. Then we execute the commands in sequence through a loop, which generates our macro command, and starts multiple tasks with one command.

The command mode actually defines a command object. The request publisher passes in parameters in a parameterized form to perform specific operations, thereby decoupling the request publisher and the receiver.

Final words:

This series of articles covers ten commonly used JavaScript design patterns. I have referred to a lot of information and combined it with my own understanding to explain it to you in the most understandable way. Due to my limited level and energy, please point out any misunderstandings in time. The design pattern series of articles will be put on hold for the time being and will be supplemented later. Next month, I will start to prepare to systematically study ES6 and complete the ES6 series of articles.

The above is the details of the command pattern of JavaScript design pattern. For more information about JavaScript design pattern, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • JavaScript Design Patterns – Command Pattern Principles and Usage Example Analysis
  • JavaScript design pattern command pattern example analysis
  • Analysis of the concept and usage of command mode in JS design pattern
  • JavaScript design pattern classic command pattern
  • In-depth understanding of JavaScript series (34): Detailed explanation of the command mode of design pattern
  • JavaScript design pattern command mode

<<:  Using vsftp to build an FTP server under Linux (with parameter description)

>>:  MySQL million-level data paging query optimization solution

Recommend

How to redirect to https through nginx load balancing

Copy the certificate and key on the web scp -rp -...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

Several scenarios for using the Nginx Rewrite module

Application scenario 1: Domain name-based redirec...

Detailed explanation of the use of Linux lseek function

Note: If there are any errors in the article, ple...

Docker installs Redis and introduces the visual client for operation

1 Introduction Redis is a high-performance NoSQL ...

How to force vertical screen on mobile pages

I recently wrote a mobile page at work, which was...

Analysis and solution of a.getAttribute(href,2) problem in IE6/7

Brief description <br />In IE6 and 7, in a ...

How to change the encoding to utf-8 in mysql version 5.7 under windows

Preface I just started learning MySQL and downloa...

Detailed explanation of the use of HTML header tags

HTML consists of two parts: head and body ** The ...

The difference between MySQL database stored procedures and transactions

Transactions ensure the atomicity of multiple SQL...

XHTML Tutorial: The Difference Between Transitional and Strict

In fact, XHTML 1.0 is divided into two types (thr...