JavaScript Design Patterns: typical builder patterns _ javascript skills

Source: Internet
Author: User
The builder mode is a design mode that separates the construction of a complex object from its representation so that different representations can be created during the same construction process. Next, I will introduce you to the JavaScript design mode classic builder mode through this article. if you are interested, let's study it together. 1. concepts of builder mode

The builder mode separates the construction of a complex object from its representation, so that different representations can be created for the same build 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. The builder mode is actually a conductor, a builder, and a customer who uses the conductor to call a specific builder to produce results.

The builder mode is mainly used to "build a complex object by Step", where "step by step" is a stable algorithm, while all parts of a complex object change frequently.

In layman's terms, Bai Fumei needs to build a villa and then directly find the contractor's head. then, the contractor needs to find a worker to build the villa. In this case, Bai Fumei does not need to find one worker. The contractor knows the needs of Bai Fumei and where to find the workers. The workers can work, saving the communication cost between Bai Fumei and the workers, bai Fumei does not need to know how to build the house. he can finally get the house.

Builder mode structure

II. Functions and precautions of builder mode

Role of the mode:

1. create a complex object step by step

2. decoupling the encapsulation process and creating components

3. no need to care about how components are assembled

Note:

1. a stable algorithm must be supported.

2. the processing technology is exposed-Bai Fumei does not have to worry about how to build a house, but he can check how the house is built at any time.

III. Builder mode code and practice Summary

 Script // 1. make a house. // 2. the contractor calls the worker to start the project, and he wants to clear a major item of the worker. // 3. A house builder can build a bedroom and a living room and a kitchen. // 4. the contractor's head is just an interface. he doesn't work. he only says that I can build the house function Fangzi () {// Fangzi can be understood as the Singleton mode if (! (This instanceof Fangzi) {return new Fangzi ();} this. woshi = ""; this. keting = ""; this. chufang = "";} function Baogongtou () {if (! (This instanceof Baogongtou) {return new Baogongtou ();} this. jianfangzi = function (gongren) {gongren. jian_chufang (); gongren. jian_keting (); gongren. jian_woshi () ;}} function Gongren () {if (! (This instanceof Gongren) {return new Gongren ();} this. jian_woshi = function () {console. log ("create bedroom");} this. jian_keting = function () {console. log ("create living room");} this. jian_chufang = function () {console. log ("create kitchen");} this. jiaofang = function () {var _ fangzi = new Fangzi (); _ fangzi. woshi = "OK"; _ fangzi. keting = "OK"; _ fangzi. chufang = "OK"; return _ fangzi;} var gongren = new Gongren (); var baogongtou = new Baogongtou (); baogongtou. jianfangzi (gongren); var myfangzi = gongren. jiaofang (); console. log (myfangzi); script

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.