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
Counting the number of a string in a file is actu...
1. Download and install the official MySQL Yum Re...
Table of contents 0. The kernel tree that comes w...
Add a DOCTYPE to the page Since different browser...
This article shares the specific code of js+Html ...
1. When to execute setUp We all know that vue3 ca...
This article shares with you how to use Vue to ch...
<br />When you click the link, the web page ...
This article shares the specific code of Vue to a...
A set of MySQL libraries for testing. The previou...
In the project, we often encounter the problem of...
Preface: In Vue, props can be used to connect ori...
Preface The method of configuring IP addresses in...
Preface The MySQL slow query log is a type of log...
I just started learning about databases recently....