Introduced
The command pattern is defined as a way to encapsulate a request into an object so that you can parameterize the customer with different requests, queue requests or log requests, and perform revocable operations. This means that the schema is designed to encapsulate the invocation, request, and operation of a function into a single object, and then perform a series of processing on the object. In addition, you can decouple the command object from the receiving object by calling the object that implements the specific function.
Body
We have come to show this pattern through the vehicle purchase process, first defining the specific operating class of the vehicle purchase:
$(function() {varCarmanager = {//Request InformationRequestinfo:function(model, ID) {return' The information for ' + model + ' with ID ' + ID + ' is foobar '; },//Buy a carBuyvehicle:function(model, ID) {return' You have successfully purchased Item ' + ID + ', a ' + model; },//Organization ViewArrangeviewing:function(model, ID) {return' You have successfully booked a viewing of ' + model + ' (' + ID + ') '; } };}) ();
Take a look at the code above and simply execute the manager's command by calling the function, but in some cases we don't want to invoke the method directly inside the object. This increases the dependency between objects and objects. Now let's extend this carmanager so that it can accept any processing request from a Carmanager object, including the model and car ID. Based on the definition of the command pattern, we would like to implement a call to this function as follows:
Carmanager.execute ({commandtype: "Buyvehicle", Operand1: ' Ford Escort ', Operand2: ' 453543 '});
According to this demand, we can implement the Carmanager.execute method:
function (command) { return carmanager[command.request] (Command.model, Command.carid);};
After the transformation, the call is much simpler, the following calls can be implemented (of course, some of the exception details need to be perfected):
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
Command mode is easier to design a command queue, it is easier to count commands in the case of requirements, and allows the party that accepts the request to decide whether or not it needs to be called, and can implement revocation and reset of the request, and because the new specific class does not affect the other classes, it is easy to implement.
But agile development principles tell us not to add guesswork-based, actually unwanted features to your code, and if you don't know if a system needs a command pattern, don't worry about implementing it, in fact, it's not difficult to implement this pattern by refactoring when needed, only when real demand such as undo and redo functions Refactoring the original code into a command pattern makes sense.
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
In-depth understanding of JavaScript series (34): Design mode Command mode