Home  >  Article  >  Web Front-end  >  js design patterns: what is command pattern? Introduction to js command mode

js design patterns: what is command pattern? Introduction to js command mode

不言
不言Original
2018-08-17 16:26:401480browse

This article brings you content about js design patterns: What is the command pattern? The introduction of js command mode has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is command mode?

Definition: Encapsulates a request into an object, allowing you to parameterize the client with different requests.

Main solution:In software systems, behavior requesters and behavior implementers usually have a tightly coupled relationship, but in some cases, such as the need to record, cancel, or This kind of tightly coupled design that cannot resist changes is not suitable when redoing, transactions, etc. are processed.

When to use: In some situations, such as "recording, undo/redo, transaction" and other processing of behaviors, this kind of tight coupling that cannot resist changes is inappropriate. of. In this case, how to decouple "behavior requester" from "behavior implementer"? Abstracting a set of behaviors into objects can achieve loose coupling between them.

How to solve: Call the acceptor through the caller to execute the command, in the order: caller→recipient→command.

Key code: Define three roles: 1. received real command execution object 2. Command 3. invoker uses the entrance of the command object

js command Advantages of the pattern: 1. Reduces system coupling. 2. New commands can be easily added to the system.

Disadvantages of js command mode: Using command mode may cause some systems to have too many specific commands.

js command mode usage scenarios: Command mode can be used wherever a command is considered, for example: 1. Every button in the GUI is a command. 2. Simulate CMD.

Note: The system needs to support the undo (Undo) operation and recovery (Redo) operation of the command. You can also consider using the command mode. See the extension of the command mode.

js command mode

The command mode is also relatively simple in JavaScript. The button and command are separated in the following code, so it can In complex projects, you can use the command mode to hand over the interface code and functional code to different people to write.

const setCommand = function(button, command) {
    button.onClick = function() {
        command.excute()
    }
}

// --------------------  上面的界面逻辑由A完成,下面的由B完成

const menu = {
    updateMenu: function() {
        console.log('更新菜单')
    },
}

const UpdateCommand = function(receive) {
    return {
        excute: receive.updateMenu,
    }
}

const updateCommand = UpdateCommand(menu) // 创建命令

const button1 = document.getElementById('button1')
setCommand(button1, updateCommand)

Related recommendations:

js design pattern: What is the observer pattern (publish-subscribe pattern)? Introduction to js observer pattern

#js design pattern: What is the iterator pattern? Introduction to js iterator pattern

The above is the detailed content of js design patterns: what is command pattern? Introduction to js command mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn