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