JavaScript design pattern classic command pattern

  • 2021-01-02 21:45:35
  • OfStack

1. Command mode concept

The command pattern (Command) is defined to parameterize and pass method calls so that they can be executed whenever needed. That is, the pattern is designed to encapsulate function calls, requests, and operations into a single 1 object, which is then processed by 1 column. It can also be used to eliminate the coupling between the object calling the operation and the object implementing the operation. This provides great flexibility for changing specific classes.

2. Functions and matters needing attention of command mode

Role of model:

1. Combine the package, request and call of functions into body 1

2. Call specific functions to decouple the command object from the receive object

3. Improve the flexibility of program modularization

Notes:

1, there is no need to excuse 1, directly call the function, so as not to cause waste

3. Command mode code and practice summary


<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>

<script>
//1.1 The company had artillery and infantry, and the commander could give orders to move troops to fight 
var lian = {};
lian.paobing = function(pao_num){
console.log(pao_num+" The gun is ready for battle ");
}
lian.bubing = function(bubing_num){
console.log(bubing_num+" Man ready for battle ");
}
lian.lianzhang = function(mingling){
lian[mingling.type](mingling.num);
}
// Order from the commander 
lian.lianzhang({
type:"paobing",
num:10
});
lian.lianzhang({
type:"bubing",
num:100
});
</script>
</body>
</html>

The above is the classic command mode of JavaScript design mode introduced by this site, I hope to help you!


Related articles: