introduce
Command mode is defined as: used to encapsulate a request into an object, so that you can parameterize the client with different requests; queue or log requests, and perform revocable operations. In other words, the change mode is designed to encapsulate the call, request and operation of a function into a single object, and then perform a series of processing on the object. In addition, the command object and the receiving object can be decoupled by calling objects that implement specific functions.
text
Let’s show this pattern through the vehicle purchase program, first defining the specific operation class of vehicle purchase:
The code copy is as follows:
$(function () {
var CarManager = {
// Request information
requestInfo: function (model, id) {
return 'The information for ' + model +
' with ID ' + id + ' is foobar';
},
// Buy a car
buyVehicle: function (model, id) {
return 'You have successfully purchased Item '
+ id + ', a ' + model;
},
// Organize view
arrangementViewing: function (model, id) {
return 'You have successfully booked a view of '
+ model + ' ( ' + id + ' ) ';
}
};
})();
Let’s take a look at the above code. We simply execute manager commands by calling functions. However, in some cases, we do not want to directly call methods inside the object. This will increase the dependence between objects. Now let's expand this CarManager to accept any processing requests from CarManager objects including model and car ID. According to the definition of the command pattern, we hope to implement the following function calls:
The code copy is as follows:
CarManager.execute({ commandType: "buyVehicle", operand1: 'Ford Escort', operand2: '453543' });
According to this requirement, we can implement the CarManager.execute method in this way:
The code copy is as follows:
CarManager.execute = function (command) {
return CarManager[command.request](command.model, command.carID);
};
After the transformation, the call is much simpler, and the following calls can be implemented (of course, some exception details still need to be improved):
The code copy is as follows:
CarManager.execute({ request: "arrangeViewing", model: 'Ferrari', carID: '145523' });
CarManager.execute({ request: "requestInfo", model: 'Ford Mondeo', carID: '543434' });
CarManager.execute({ request: "requestInfo", model: 'Ford Escort', carID: '543434' });
CarManager.execute({ request: "buyVehicle", model: 'Ford Escort', carID: '543434' });
Summarize
The command mode is easier to design a command queue, and it is easier to count commands into the log when required, and allows the party that accepts the request to decide whether to call it, and can also implement revocation and reset of the request. Moreover, since the added specific classes do not affect other classes, it is easy to implement.
But the principle of agile development tells us not to add guess-based and actually unwanted functions to the code. If you are not sure whether a system needs a command mode, you should generally not rush to implement it. In fact, it is not difficult to implement this mode when it is required. It is only meaningful to refactor the original code into a command mode when it is truly required such as undoing, restoring operations and other functions.