OverviewThe builder pattern is a relatively simple design pattern and belongs to a creational pattern. Definition: Decompose a complex object into multiple simple objects for construction, separate the complex construction layer from the presentation layer, so that the same construction process can create different presentation modes; advantage:
Mode function:
Note:
Vernacular explanation: A rich man wants to build a house. He only needs to find a contractor, who will then find a construction team to build the house. He does not need to find workers one by one to build a construction team and start construction. The contractor knows the rich man's needs and also knows where to find workers to build a construction team. The workers can work directly, saving the rich man the cost of communicating directly with the workers. The rich man does not need to know how to build a house. He only needs to be able to inspect the house in the end. Code ImplementationBefore writing the code, let's analyze it first: 1. The output is a house 2. The contractor calls the workers to start the work and he must be very clear about the specific major project of the workers. 3. Workers are house builders who can build bedrooms, living rooms and kitchens. 4. The contractor is just an interface. He only tells others to build a house, but he doesn't have to do anything. function Fangzi(){ this.woshi = ""; this.keting = ""; this.chufang = ""; } function Baogongtou(){ this.jianfangzi = function(gongren){ gongren.jian_woshi(); gongren.jian_keting(); gongren.jian_chufang(); } } function Gongren(){ this.jian_woshi = function(){ console.log("The bedroom is built!"); } this.jian_keting = function(){ console.log("The living room is built!"); } this.jian_chufang = function(){ console.log("The kitchen is built!"); } this.wangong = function(){ var fangzi = new Fangzi(); fangzi.woshi = "ok"; fangzi.keting = "ok"; fangzi.chufang = "ok"; return fangzi; } } let gongren = new Gongren(); let baogongtou = new Baogongtou(); //The bedroom is finished! //The living room is built! //The kitchen is built! baogongtou.jianfangzi(gongren); var my_fangzi = gongren.wangong(); /* Fangzi= Chufang: "ok" keting: "ok" woshi: "ok" } */ console.log(my_fangzi); In the above code, we can see that Gongren() contains the specific construction process, that is, the specific things to do. Fangzi() is empty at the beginning, without a living room, kitchen, or bedroom. Baogongtou() only advertises that a house can be built, and then passes in the worker method to call the worker to do the construction. When the worker method is executed, the construction is completed, and then the house is handed over. Instantiate the Fangzi() method in a new method and reassign it in the new method. The above is the details of how to implement the builder pattern with Javascript. For more information about the Javascript builder pattern, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to install PHP7 Redis extension on CentOS7
>>: How to create a file system in a Linux partition or logical volume
Preface Introduction Lombok is a handy tool, just...
Table of contents 1. Introduction 2. Customize ta...
Official documentation: JSON Functions Name Descr...
Table of contents Preface Implementation ideas Ef...
Prerequisite: Mac, zsh installed, mysql downloade...
The optimization created by MySQL is to add index...
1. Pull the official 5.0.3 image [root@localhost ...
Table of contents 1. Steps to use Jquery: (1) Imp...
1. The error information reported when running th...
Table of contents Installation Environment Descri...
Page domain relationship: The main page a.html bel...
Preface Previously, I talked about the problem of...
Date-type single-row functions in MySQL: CURDATE(...
1. Compile proto Create a new proto folder under ...
If every company wants to increase its user base,...