A deep understanding of the JavaScript series (27): The Builder mode of design patterns, and a deep understanding of javascript

Source: Internet
Author: User

A deep understanding of the JavaScript series (27): The Builder mode of design patterns, and a deep understanding of javascript
Introduction

In a software system, sometimes it is faced with the creation of "a complex object", which is usually composed of sub-objects of each part using a certain algorithm; due to changes in requirements, the various 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 can we provide a "encapsulation mechanism" to isolate the changes of "various parts of complex objects", so as to keep the "stable Construction Algorithm" in the system from changing with demand? This is the builder mode.

The builder mode separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. That is to say, if we use the builder mode, users need to specify the types to be built to get them, and the specific construction process and details do not need to be known.

Body

This mode is relatively simple. first go to the code and then explain it.

Function getBeerById (id, callback) {// use ID to request data, and then return data. asyncRequest ('get', 'beer. uri? Id = '+ id, function (resp) {// callback call response callback (resp. responseText) ;}) ;}var el = document. querySelector ('# test'); el. addEventListener ('click', getBeerByIdBridge, false); function getBeerByIdBridge (e) {getBeerById (this. id, function (beer) {console. log ('requested Beer: '+ beer );});}

According to the builder's definition, the table phase is callback. That is to say, how to display and process data after obtaining data depends on the callback function. When processing data, the corresponding callback function does not need to focus on how to obtain data, in the same example, we can see in jquery's ajax method that there are many callback functions (such as success and error callback), with the main purpose of separation of duties.

Let's use another jQuery example:

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

We only need to input the HTML characters to be generated, instead of how the specific HTML object is produced.

Summary

The builder mode is mainly used to "build a complex object by step". In this mode, "Step by Step" is a stable algorithm, while each part of the complex object changes frequently. Its advantages include: the "processing process" of the builder mode is exposed, which makes the builder mode more flexible, and the builder mode decouples the assembly process and creates specific parts, so that we don't have to worry about how each part is assembled.

Copyright statement: This article is the original author of the http://www.zuiniusn.com, not allowed by the blogger can not be reproduced.

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.