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

Source: Internet
Author: User

Introduced

In the software system, sometimes facing the creation of "a complex object", it is usually composed of the sub-objects of each part with certain algorithms, and because of the change of the 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.

Body

This mode is relatively simple, first on the code, and then explained

functionGetbeerbyid (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.

Here's another example of jquery:

$ ('<class= ' foo '></div>');

We only need to pass in the HTML characters that we want to generate, without having to relate to how the specific HTML object is produced.

Summarize

The builder pattern is primarily used to "step through a complex object" in which the "step-by" is a stable algorithm, while the parts of the complex object change frequently, with the advantage 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.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

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.