In-depth understanding of the JavaScript series (27): Design Pattern Builder mode

Source: Internet
Author: User

In the software system, sometimes faced with the creation of "a complex object", it is usually made up of the sub-objects of each part with certain algorithms, and because of the change of requirements, the parts of this complex object often face drastic changes, but the algorithms that combine them are relatively stable. How to deal with this change? How do you provide a "encapsulation mechanism" to isolate changes in "parts of complex objects" so that the "stable build algorithm" in the system does not change as demand changes? This is the builder model to say. The builder pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations. In other words, if we use the builder model, then the user needs to specify the type to be built to get them, and the concrete construction process and details do not need to know. The text is relatively simple, first on the code, then explainedfunctionGetbeerbyid (ID, callback) {//use the ID to request the data, and then return the data.Asyncrequest (' GET ', ' beer.uri?id= ' + ID,function(resp) {//Callback Call Responsecallback (Resp.responsetext); });}varel = Document.queryselector (' #test '); El.addeventlistener (' Click ', Getbeerbyidbridge,false);functionGetbeerbyidbridge (e) {Getbeerbyid ( This. ID,function(beer) {Console.log (' Requested Beer: ' +beer); });} According to the builder definition, the table is a callback, that is, how to get the data after the display and processing depends on the callback function, the corresponding callback function when processing the data does not need to focus on how to obtain data, the same example can be seen in the Ajax method of jquery, There are many callback functions (such as success, error callback, etc.), the main purpose is separation of duties. Again another example of jquery: $ (' <div class= ' foo ' > Bar </div> 'we only need to pass in the HTML characters to be generated, rather than how the specific HTML object is produced. The summary builder pattern is primarily used to "step through a complex object", in which "step-by" is a stable algorithm, while the parts of complex objects change frequently, and the advantage is that the "machining process" of the builder pattern is exposed, which makes the builder model more flexible, and the builder mode decouples the assembly process and creates the specific parts, so we don't have to care about how each part is assembled. Reference: http://www.cnblogs.com/feipeng/archive/2007/03/12/671932.htmlsynchronization and recommendation this article has been synchronized to the directory index: in-depth understanding of JavaScript series in-depth understanding of the JavaScript series, including the original, translation, reprint and other types of articles, if it is useful to you, please recommend supporting a, to the power of the uncle writing. 

In-depth understanding of the JavaScript series (27): Design Pattern Builder mode

Related Article

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.