Deep understanding of JavaScript series (34): Command mode in Design Mode

Source: Internet
Author: User
Introduction

Command is used to encapsulate a request into an object, so that you can parameterize the customer with different requests; queue requests or record request logs, and perform unrecoverable operations. That is to say, the pattern change aims to encapsulate function calls, requests, and operations into a single object, and then process this object in a series. In addition, you can call the object that implements a specific function to decouple the command object and the receiving object.

Body

We will purchase it through the vehicleProgramTo demonstrate this mode, first define the specific operation class of the vehicle purchase:

$ ( Function (){

VaR Carmanager = {

// Request Information
Requestinfo: Function (Model, ID ){
Return 'The information' + model +
'With id' + ID + 'is foobar ';
},

// Buy a car
Buyvehicle: Function (Model, ID ){
Return 'You have successfully purchased item'
+ ID + ', A' + model;
},

// Organization view
Arrangeviewing: Function (Model, ID ){
Return 'You have successfully booked a viewing'
+ Model + '(' + ID + ')';
}
};
})();

Let's take a look at the aboveCodeYou can call a function to simply execute the Manager Command. However, in some cases, we do not want to directly call the internal methods of the object. This will increase the dependency between objects. Now let's extend this carmanager so that it can accept any processing requests from carmanager objects including model and car ID. According to the definition of the command mode, we want to call the following functions:

 
Carmanager.exe cute ({commandtype: "buyvehicle", operand1: 'Ford escort', operand2: '2013 '});

To meet this requirement, we can find the carmanager.exe cute method as follows:

 
Carmanager.exe cute =Function(Command ){
ReturnCarmanager [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 need to be improved ):

 
Carmanager.exe cute ({request :"Arrangeviewing", Model :'Ferrari', Carid :'145523'});
Carmanager.exe cute ({request :"Requestinfo", Model :'Ford Mondeo', Carid :'543434'});
Carmanager.exe cute ({request :"Requestinfo", Model :'Ford Escort', Carid :'543434'});
Carmanager.exe cute ({request :"Buyvehicle", Model :'Ford Escort', Carid :'543434'});

Summary

The command mode is easier to design a command queue. When required, it is easier to include the command into the log, and the requesting party is allowed to decide whether to call the command, in addition, the request can be revoked and reset, and the added classes do not affect other classes, so it is easy to implement.

However, the agile development principle tells us that we should not add functions that are based on speculation and are not actually needed for the code. If we do not know whether a system requires a command mode, we should not try to implement it in a hurry, as a matter of fact, it is not difficult to implement this mode through refactoring when needed. it is meaningful to reconstruct the original code into the command mode only when you really need functions such as Undo and restore operations.

Transferred from: Uncle Tom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.